Mock CloudBlobClient with AutoFac and AutoMock

后端 未结 3 528
误落风尘
误落风尘 2021-01-14 10:07

I\'m trying to write unit tests for my AzureBlobRepository. The repository receives a CloubBlobClient in the constructor. I want to mock the client, but this gives an except

3条回答
  •  不知归路
    2021-01-14 10:50

    I have managed to mock it using NSubstitute, i have only mocked the functions that i use.

    /// 
    /// Create a mock for CloudBlobClient
    /// 
    /// 
    /// 
    private CloudBlobClient GetMock(bool containerExists = true)
    {
        var items = new List();
        var client = Substitute.For(new Uri("http://foo.bar/"), null);
        var container = Substitute.For(new Uri("http://foo.bar/"));
        client.GetContainerReference(Arg.Any()).Returns(container);
        container.ExistsAsync(Arg.Any()).Returns(Task.FromResult(containerExists));
        container.ListBlobsSegmentedAsync(Arg.Any(), Arg.Any(), 
                                            Arg.Any(), 
                                            Arg.Any(), 
                                            Arg.Any(), 
                                            Arg.Any(), 
                                            Arg.Any(), 
                                            Arg.Any())
                                            .Returns(ci => new BlobResultSegment(items.ToArray(), null));
    
        container.GetBlockBlobReference(Arg.Any()).Returns(ci => GetBlockBlobMock(ci.ArgAt(0), items));
        return client;
    }
    
    /// 
    /// Create a mock for CloudBlockBlob
    /// 
    /// 
    /// 
    /// 
    private CloudBlockBlob GetBlockBlobMock(string name, List listBlobItems)
    {
        var created = DateTimeOffset.Now;
        var bufferStream = new MemoryStream();
        var blob = Substitute.For(new Uri("https://foo.blob.core.windows.net/bar/" + name + ".txt"));
        //We can't mock the value the normal way, use reflection to change its value!
        blob.Properties.GetType().GetProperty(nameof(blob.Properties.Created)).SetValue(blob.Properties, created);
        //we cant mock properties! (Dam this wont work)
        blob.UploadFromStreamAsync(Arg.Any(),
                                    Arg.Any(),
                                    Arg.Any(),
                                    Arg.Any(),
                                    Arg.Any()).Returns(ci => {
                                        var stream = ci.Arg();
                                        stream.CopyTo(bufferStream);
                                        listBlobItems.Add(blob);
                                        return Task.CompletedTask;
                                    });
    
        blob.DownloadToStreamAsync(Arg.Any(),
                                    Arg.Any(),
                                    Arg.Any(),
                                    Arg.Any(),
                                    Arg.Any()).Returns(ci =>
                                    {
                                        var stream = ci.Arg();
                                        bufferStream.Position = 0;
                                        bufferStream.CopyTo(stream);
                                        stream.Position = 0;
                                        return Task.CompletedTask;
                                    });
        return blob;
    }
    

    I'm not 100% sure its 100% accurate but it allows me to run some unit tests!

提交回复
热议问题