Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Mock BlobContainerClient() of Azure.Storage.Blobs?

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;
        }
    }
like image 713
Ashraf Avatar asked Oct 16 '20 07:10

Ashraf


People also ask

How do you use BlobContainerClient?

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.

What is default size of block blobs?

Storage clients default to a 128 MiB maximum single blob upload, settable in the Azure Storage client library for .


1 Answers

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

like image 160
Sergey Solianyk Avatar answered Oct 13 '22 11:10

Sergey Solianyk