Hey there Angela Calborean
Thats a good question and thanks for using QandA platform.
So Since Moq doesn't support mocking extension methods directly, it's best to abstract away the creation of BlobBatchClient
in your production code. Create an interface that represents the functionality you need from BlobBatchClient
, and then implement that interface in a class that internally uses BlobBatchClient
.
Inject the interface representing the BlobBatchClient
functionality into the classes.
public interface IBlobBatchClientWrapper
{
Task SetBlobsAccessTierAsync(IEnumerable<Uri> blobUris, AccessTier tier, CancellationToken cancellationToken = default);
}
public class BlobBatchClientWrapper : IBlobBatchClientWrapper
{
private readonly BlobBatchClient _blobBatchClient;
public BlobBatchClientWrapper(BlobServiceClient blobServiceClient)
{
_blobBatchClient = blobServiceClient.GetBlobBatchClient();
}
public async Task SetBlobsAccessTierAsync(IEnumerable<Uri> blobUris, AccessTier tier, CancellationToken cancellationToken = default)
{
// Delegate the actual functionality to the BlobBatchClient instance
await _blobBatchClient.SetBlobsAccessTierAsync(blobUris, tier, cancellationToken);
}
}
Now, in your production code, use IBlobBatchClientWrapper
instead of directly using BlobBatchClient
.
For your unit tests, you can now easily mock IBlobBatchClientWrapper
using Moq
var blobBatchClientWrapperMock = new Mock<IBlobBatchClientWrapper>();
blobBatchClientWrapperMock.Setup(m => m.SetBlobsAccessTierAsync(It.IsAny<IEnumerable<Uri>>(), It.IsAny<AccessTier>(), It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);
var myService = new MyService(blobBatchClientWrapperMock.Object);
If this helps kindly accept the answer thanks much.