【问题标题】:How to mock CloudBlockBlob ExistsAsync() method如何模拟 CloudBlockBlob ExistsAsync() 方法
【发布时间】:2019-11-25 05:09:51
【问题描述】:

我正在使用 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() 中使用如下,那么它将返回模拟数据。但我不能在下面使用,因为我正在根据输入CloudBlockBlobReturnFileValue() 的值检查文件是否存在。

var value = this._blockBlob.ExistsAsync()

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

添加了截图供参考。

【问题讨论】:

    标签: .net .net-core moq xunit


    【解决方案1】:

    要模拟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));
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-30
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      相关资源
      最近更新 更多