简体   繁体   中英

How to mock Azure Storage Account in C# using Moq?

I have a service IBlobService :

public interface IBlobService
{
    public Task<BlobInfo> GetBlobAsync(string name);
    public Task<IEnumerable<string>> ListBlobsAsync();
    public Task UploadFileBlobAsync(string filePath, string fileName);
    public Task UploadContentBlobAsync(string content, string fileName);
    public Task DeleteBlobAsync(string blobName);
}

Here is the implementation of the GetBlobAsync method within my BlobService class:

public async Task<BlobInfo> GetBlobAsync(string name)
{
    var client = _blobContainerClient.GetBlobClient(name);
    var blobDownloadInfo = await client.DownloadContentAsync();
    return new BlobInfo(blobDownloadInfo.Value.Content, blobDownloadInfo.Value.Details.ContentType);
}

I am trying to figure out how I can use Moq to implement my test. I am relatively new to Moq. Any ideas or guidance?

I have tried to review this blog https://devblogs.microsoft.com/azure-sdk/unit-testing-and-mocking/

but i'm still finding myself stuck. I am trying to figure out the best way to implement my test.

If you want to make a unit test for testing the blobservice or a class using the IblobService ?

If it's a class using your IBlobService then you need to mock the IBlobService

like this


...
public void Test1() {
   var blobServiceMocked = new Mock<IBlobService>();
   blobServiceMocked.Setup(e => e.GetBlobAsync(It.IsAny<string>()).Returns(new BlobInfo(){ ... }); 
blobServiceMocked.Object;
... 

// assert
  _methodThatTakesIBlobservice(blobServiceMocked.Object);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM