首页 > 解决方案 > 如何模拟 CloudBlockBlob ExistsAsync() 方法

问题描述

我正在使用 xUnit 和 Moq 编写单元测试。

我的测试类TestBlobServiceProvider构造函数具有以下代码

  private readonly Common.Interfaces.IBlobServiceProvider _iblobprovider;
    public TestBlobServiceProvider()
    {


        var mockCloudBlobContainer = new Mock<IBlobOperations>();
        mockCloudBlobContainer.Setup(repo => repo.parseConnString(It.IsAny<string>())).Returns(new CloudStorageAccount(new StorageCredentials(), new Uri("http://mytest"), new Uri("http://mytest"), new Uri("http://mytest"), new Uri("http://mytest")));

        var blobMock = new Mock<CloudBlockBlob>(new Uri("http://tempuri.org/blob"));
        blobMock
            .Setup(m => m.ExistsAsync())
            .ReturnsAsync(true);
        _iblobprovider = new BlobServiceProvider(mockCloudBlobContainer.Object, blobMock.Object);


    }

以下是测试方法

    [Fact]
    public void DownloadFromBlob_Success()
    {
        string containerName = "d";
        string fileName = "w";
        string downloadPath = "e";
        _iblobprovider.DownloadFromBlob(containerName,fileName,downloadPath);
    }

BlobServiceProvider类具有以下代码

 public readonly CloudBlobClient blobClient = null;
 private readonly IBlobOperations _blobOperations;
 public   CloudBlockBlob _blockBlob = null;

 public BlobServiceProvider(IBlobOperations blobOperations,CloudBlockBlob  cloudBlockBlob )
        {
            string storageConnectionString="";
            this._blobOperations = blobOperations;
              this._blockBlob= cloudBlockBlob; // this._blockBlob contains ExistsAsync() mock data which was created in TestBlobServiceProvider constructor.How can I return mock data if ExistsAsync() called.
            CloudStorageAccount storageAccount = this._blobOperations.parseConnString(storageConnectionString);
            blobClient = storageAccount.CreateCloudBlobClient();
        }

      public void DownloadFromBlob(string containerName, string fileName, string downloadPath)
        {
            CloudBlockBlob file = GetBlockBlobContainer(containerName).GetBlockBlobReference(fileName);
            var val = ReturnFileValue(file); 
            if (!val)
                throw new FileNotFoundException($"{Messages.ExMethodName} {MethodBase.GetCurrentMethod()} {Messages.ExMessage} {Messages.FileNotFound}");

            file.DownloadToFileAsync(Path.Combine(downloadPath, fileName), FileMode.Create);
        }

      private bool ReturnFileValue(CloudBlockBlob file)
        {
            var value = file.ExistsAsync(); // here I need to return mock data, but I'm not getting how can I use this._blockBlob here

            return value.Result;
        }

我知道如果我在下面使用ReturnFileValue(),那么它将返回模拟数据。但我不能在下面使用,因为我正在根据输入CloudBlockBlob值 检查文件是否存在ReturnFileValue()

var value = this._blockBlob.ExistsAsync()

那么,我怎样才能 var value = file.ExistsAsync();返回模拟数据。

添加截图以供参考。

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

标签: .net.net-coremoqxunit

解决方案


要模拟该ExistAsync方法,请检查下面的代码是否对您有帮助:

public Mock<ShareClient> shareClient = new Mock<ShareClient>();

并且该Exist方法可以被嘲笑为:

shareClient.Setup(s => s.ExistsAsync(It.IsAny<CancellationToken>())).ReturnsAsync(Response.FromValue<bool>(true, null));    

现在,无论何时调用shareClient.ExistsAsync(),它都会返回true,就像你可以模拟它返回一样false

与方法相同ShareDirectoryClient ExistAsync

public Mock<ShareDirectoryClient> shareDirectoryClient = new Mock<ShareDirectoryClient>();
shareDirectoryClient.Setup(s => s.ExistsAsync(It.IsAny<CancellationToken>())).ReturnsAsync(Response.FromValue<bool>(true, null));

我希望这会有所帮助。


推荐阅读