How to mock or unit test AzureStorageAccount with xunit

sachin 1 Reputation point
2023-06-22T15:48:07.8166667+00:00
Hi, 

Want to unit test below code:


CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connString");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("containerName");
CloudBlockBlob cloudBlobInstance = container.GetBlockBlobReference(container, "imageName");
cloudBlobInstance.UploadFromByteArrayAsync(ImageBytes, 0, ImageByt

and to do this I move below code to a wrapper like:

public class AzureServiceWrapper : IAzureServiceWrapper
    {
        public CloudStorageAccount GetCloudStorageAccount(string connString)
        {
            CloudStorageAccount cloudStorageAccountClient = CloudStorageAccount.Parse(connString);
            return cloudStorageAccountClient;
        }
        public CloudBlockBlob GetBlockBlobReference(CloudBlobContainer storageContainer, string fileName)
        {
            return storageContainer.GetBlockBlobReference(fileName);
        }
          public CloudBlobClient GetBlobClient(CloudStorageAccount cloudStorageAccountClient)
        {      
            CloudBlobClient cloudBlobClient = cloudStorageAccountClient.CreateCloudBlobClient();
            return cloudBlobClient;
        }    

         public CloudBlobContainer GetBlobContainer(CloudBlobClient cloudBlobClient, string containerName)
        {     CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
            return cloudBlobContainer;
        }
    }
}

And then using this interface I can Inject it and modify my repo code like:

CloudStorageAccount cloudStorageAccountClient = _azureSeriveWrapper.GetStorageAccount("connString"); 
CloudBlobClient cloudBlobClient  = _azureSeriveWrapper.GetBlobClient(cloudStorageAccountClient); 
CloudBlobContainer cloudBlobContainer= _azureSeriveWrapper.GetBlobContainer(cloudBlobClient,"containerName");
 CloudBlockBlob cloudBlobInstance = _azureSeriveWrapper.GetBlockBlobReference(cloudBlobContainer, AssetImageName); 
 cloudBlobInstance.UploadFromByteArrayAsync(ImageBytes, 0, ImageBytes.Length); 
 mediaUrl = cloudBlobInstance.Uri.AbsoluteUri;

But how to mock this in unit test method if above is correct or please help me with the sample code for the same.

Thanks in advance,

Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,551 questions
Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,401 Reputation points Volunteer Moderator
    2023-06-22T20:02:22.6266667+00:00

    you need a wrapper for all the Storage objects (CloudStorageAccount, CloudBlockBlob, etc) that your wrapper returns.

    a better approach is to abstract all the methods you will use to return poco objects.

    mocking generally depends on interface design or class inheritance. so a mock of IAzureServiceWrapper would return mock objects. for example GetCloudStorageAccount would return a mock CloudStorageAccount(). if you write the mocks yourself you the mock class inherits from CloudStorageAccount and would implements all the methods called by test.

    TDD usually requires designs the objects to be easily testable.


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.