.net - 如何模拟 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();
返回模拟数据。
添加截图以供参考。
解决方案
要模拟该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));
我希望这会有所帮助。
推荐阅读
- python - ValueError:预期的二维数组,而不是一维数组。训练模型
- django - 为单个 Django 查询关闭 Postgres 并行查询
- python - 如何在 Python 中过滤/清理列表
- android - 没有 LiveData 的房间
- python - 在 for 循环中排除数据集的某些列
- flask - Flask-WTF 在它应该失败时通过验证
- php - 在 WooCommerce 结帐时格式化帐单电话号码
- java - 选择项目时的javafx listview空指针
- rest - 如何使用 Pydio Cells API 将文件/文件夹上传到 Pydio Cells
- graphql - “文件”类型的 GraphQL 错误字段“图像”必须具有子字段选择。您的意思是“图像 { ... }”吗?