'C#, How to mock Azure Storage Method and Properties in nUnit tests
I am working on nUnit tests for .NET CORE 3.1 Azure function with Azure Storage. I need to mock Azure Storage and need help on that!
I get collection of Azure Storage Files Microsoft.WindowsAzure.Storage.File.CloudFile in object fileList and then I am looping through it. Then I am calling FetchAttributesAsync() How can I mock FetchAttributesAsync, SetMetadataAsync & ContentMD5
if (fileList.Any())
{
foreach (var foundFile in fileList)
{
await foundFile .FetchAttributesAsync();
foundFile .Metadata.Add("Discovered", "true");
await discoveredFile.SetMetadataAsync();
var md5Hash = foundFile .Properties.ContentMD5;
}
Solution 1:[1]
CloudFile doesn't seem to implement those methods from any interface.
So, if you really want to mock it, you will need to wrap CloudFile. See below for the basic idea. Then you can mock ICloudFile.
public interface ICloudFile
{
Task FetchAttributesAsync();
}
public class CloudFileWrapper : ICloudFile
{
private readonly CloudFile _impl;
public Task FetchAttributesAsync() => _impl.FetchAttributesAsync();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
