Reputation: 377
I need some example of creating Mock object for BlobContainerClient of Azure.Storage.Blobs for unit testing. How can I create Mock for following class?
public sealed class BlobStorageProcessor
{
public BlobStorageProcessor(ILogger logger, BlobContainerClient containerClient)
{
this.logger = logger;
this.containerClient = containerClient;
}
}
Upvotes: 15
Views: 28326
Reputation: 381
Microsoft have now covered this in a blog post: https://devblogs.microsoft.com/azure-sdk/unit-testing-and-mocking/
Basically, you use the Moq
package to create a mock object and setup methods/properties that will be used by BlobStorageProcessor
.
public static BlobContainerClient GetBlobContainerClientMock()
{
var mock = new Mock<BlobContainerClient>();
mock
.Setup(i => i.AccountName)
.Returns("Test account name");
return mock.Object;
}
In your unit test you should inject result of GetBlobContainerClientMock
method to BlobStorageProcessor
:
var blobStorageProcessor = new BlobStorageProcessor(
GetLoggerMock(),
GetBlobContainerClientMock()
);
GetLoggerMock
could by implemented similarly to GetBlobContainerClientMock
. Read more info here: Moq Quickstart
Upvotes: 17