简体   繁体   English

如何模拟 Azure.Storage.Blobs 的 BlobContainerClient()?

[英]How to Mock BlobContainerClient() of Azure.Storage.Blobs?

I need some example of creating Mock object for BlobContainerClient of Azure.Storage.Blobs for unit testing.我需要一些为 Azure.Storage.Blobs 的 BlobContainerClient 创建 Mock 对象以进行单元测试的示例。 How can I create Mock for following class?如何为以下课程创建 Mock?

 public sealed class BlobStorageProcessor
    {
        public BlobStorageProcessor(ILogger logger, BlobContainerClient containerClient)
        {
            this.logger = logger;
            this.containerClient = containerClient;
        }
    }

Microsoft have now covered this in a blog post: https://devblogs.microsoft.com/azure-sdk/unit-testing-and-mocking/微软现在已经在一篇博文中介绍了这一点: https ://devblogs.microsoft.com/azure-sdk/unit-testing-and-mocking/

Basically, you use the Moq package to create a mock object and setup methods/properties that will be used by BlobStorageProcessor .基本上,您使用Moq包创建一个模拟对象并设置BlobStorageProcessor将使用的方法/属性。

public static BlobContainerClient GetBlobContainerClientMock()
{
    var mock = new Mock<BlobContainerClient>();

    mock
        .Setup(i => i.AccountName)
        .Returns("Test account name");

    return mock.Object;
}

In your unit test you should inject result of GetBlobContainerClientMock method to BlobStorageProcessor :在您的单元测试中,您应该将GetBlobContainerClientMock方法的结果注入BlobStorageProcessor

var blobStorageProcessor = new BlobStorageProcessor(
    GetLoggerMock(),
    GetBlobContainerClientMock()
);

GetLoggerMock could by implemented similarly to GetBlobContainerClientMock . GetLoggerMock可以通过类似于GetBlobContainerClientMock的方式实现。 Read more info here: Moq Quickstart在此处阅读更多信息:起订量快速入门

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Azure.Storage.Blobs 上传 stream - How to upload stream with Azure.Storage.Blobs Azure.Storage.Blobs 文件夹结构 - Azure.Storage.Blobs Folder Structure 如何使用 Azure.Storage.Blobs BlobClient 检索 blob 目录路径中的 blob? - How to retrieve blobs within a blob directory path using the Azure.Storage.Blobs BlobClient? 使用新的 Azure.Storage.Blobs 时如何计算存储帐户中 Blob 存储容器的总大小 - How to calculate the total size of Blob storage containers in an storage account when using the new Azure.Storage.Blobs 如何使用 Azure.Storage.Blobs 程序集对 Azure blob 存储操作设置重试策略? - How do I set a retry policy on an Azure blob storage operation using the Azure.Storage.Blobs assembly? 如何使用 C# 中的 Azure.Storage.Blobs 以 ByteArray 格式从 Azure 存储 blob 中获取文件 - How to get file from Azure storage blob in a ByteArray format using Azure.Storage.Blobs in C# 使用 Azure.Storage.Blobs 枚举大型 Azure BLOB 容器 - Enumerating large Azure BLOB containers using Azure.Storage.Blobs Azure.Storage.Blobs:找不到有效的帐户信息组合 - Azure.Storage.Blobs: No valid combination of account information found 有没有办法在 Azure.Storage.Blobs 中获取 blob 的 ContentType? - Is there a way to get a blob's ContentType in Azure.Storage.Blobs? 如何使用新的 Azure.Storage.Blobs 命名空间仅获取 blob 中的文件夹名称 - How to get only folder name in blob using new Azure.Storage.Blobs namespace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM