gpt4 book ai didi

c# - 模拟CloudStorageAccount和CloudTable for Azure表存储

转载 作者:行者123 更新时间:2023-12-02 01:09:30 25 4
gpt4 key购买 nike

因此,我试图测试Azure表存储并模拟我依赖的东西。我的类是以在构造函数中建立连接的方式构造的,即,我创建了CloudStorageAccount的新实例,在其中创建了具有StorageCredentialsstorageNamestorageKey实例。之后,创建一个CloudTable实例,在代码中进一步使用它执行CRUD操作。我的课如下:

public class TableStorage : ITableStorage
{
private const string _records = "myTable";
private CloudStorageAccount _storageAccount;
private CloudTable _table;
private ILogger<TableStorage> _logger;

public AzureTableStorageService(ILogger<TableStorage> loggingService)
{
_storageAccount = new CloudStorageAccount(new StorageCredentials(
ConfigurationManager.azureTableStorageName, ConfigurationManager.azureTableStorageKey), true);
_table = _storageAccount.CreateCloudTableClient().GetTableReference(_records);
_table.CreateIfNotExistsAsync();
_logger = loggingService;
}

//...
//Other methods here
}


_table在整个类中出于不同目的被重用。我的目标是模拟它,但是由于它是虚拟的并且没有实现任何接口,因此我无法提供简单的 Mock解决方案,例如:

_storageAccount = new Mock<CloudStorageAccount>(new Mock<StorageCredentials>(("dummy", "dummy"), true));
_table = new Mock<CloudTable>(_storageAccount.Object.CreateCloudTableClient().GetTableReference(_records));


因此,当我尝试以这种方式构建单元测试时,我得到:
Type to mock must be an interface or an abstract or non-sealed class.

我的目标是实现以下目标:

_table.Setup(x => x.DoSomething()).ReturnsAsync("My desired result");


任何想法都受到高度赞赏!

最佳答案

我还在努力实现对具有Azure表存储绑定的Azure功能的单元测试。我最终使用派生的CloudTable类工作,可以覆盖我使用的方法并返回固定的结果。

/// <summary>
/// Mock class for CloudTable object
/// </summary>
public class MockCloudTable : CloudTable
{

public MockCloudTable(Uri tableAddress) : base(tableAddress)
{ }

public MockCloudTable(StorageUri tableAddress, StorageCredentials credentials) : base(tableAddress, credentials)
{ }

public MockCloudTable(Uri tableAbsoluteUri, StorageCredentials credentials) : base(tableAbsoluteUri, credentials)
{ }

public async override Task<TableResult> ExecuteAsync(TableOperation operation)
{
return await Task.FromResult(new TableResult
{
Result = new ScreenSettingEntity() { Settings = "" },
HttpStatusCode = 200
});
}
}


我通过传递存储模拟器用于本地存储的配置字符串来实例化模拟类(请参见 https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string)。

var mockTable = new MockCloudTable(new Uri("http://127.0.0.1:10002/devstoreaccount1/screenSettings"));


在此示例中,“ screenSettings”是表的名称。

现在可以从单元测试将模拟类传递给Azure函数。

也许这就是您想要的?

关于c# - 模拟CloudStorageAccount和CloudTable for Azure表存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53510000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com