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

标签 c# azure unit-testing mocking azure-table-storage

所以我正在尝试测试 Azure 表存储并模拟我所依赖的东西。我的类的结构方式是在构造函数中建立连接,即我创建 CloudStorageAccount 的新实例。我在其中创建了 StorageCredentials 的实例有storageNamestorageKey 。然后,我创建 CloudTable 的实例,我在代码中进一步使用它来执行 CRUD 操作。我的类(class)如下所示:

public class AzureTableStorageService : ITableStorage
{
        private const string _records = "myTable";
        private CloudStorageAccount _storageAccount;
        private CloudTable _table;

        public AzureTableStorageService()
        {
            _storageAccount = new CloudStorageAccount(new StorageCredentials(
                 ConfigurationManager.azureTableStorageName, ConfigurationManager.azureTableStorageKey), true);
            _table = _storageAccount.CreateCloudTableClient().GetTableReference(_records);
            _table.CreateIfNotExistsAsync();
        }

        //...
        //Other methods here
}

_table在整个类(class)中出于不同目的而重复使用。我的目标是模拟它,但由于它是虚拟的并且没有实现任何接口(interface),所以我无法用简单的 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");

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

最佳答案

我还在努力为绑定(bind)到 Azure 表存储的 Azure Function 实现单元测试。我终于使用派生的 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://learn.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 Functions。

也许这就是您正在寻找的东西?

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

相关文章:

c# - 环绕面板 : Trying to make the ItemWidth equal to the max width of any one element

c# - 为什么 ConcurrentDictionary 有 AddOrUpdate 和 GetOrAdd,而 Dictionary 没有?

c# - 在 CloudBlobContainer 中找不到 Listblob()

javascript - Angular2 - 使用 debounceTime 测试调用

unit-testing - 模拟与测试数据库?

c# - 从十进制度转换为度分秒十分之一。

c# - 反序列化期间未找到构造函数?

azure - 在我的 blob 中找不到 wad-iis-failedreqlogfiles

python - Azure Python 函数应用程序中的 HTTP 请求

unit-testing - 在 Racket 中使用模拟对象进行猴子修补