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;
}
}
The new BlobBaseClient uses the same request policy pipeline as the BlobContainerClient. Create a new BlobClient object by appending blobName to the end of Uri. The new BlobClient uses the same request policy pipeline as the BlobContainerClient. Initializes a new instance of the BlobLeaseClient class.
Storage clients default to a 128 MiB maximum single blob upload, settable in the Azure Storage client library for .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With