gpt4 book ai didi

c# - 使用 AutoFac 和 AutoMock 模拟 CloudBlobClient

转载 作者:行者123 更新时间:2023-11-30 16:42:54 31 4
gpt4 key购买 nike

我正在尝试为我的 AzureBlobRepository 编写单元测试。存储库在构造函数中接收一个 CloubBlobClient。我想模拟客户端,但这给出了一个异常(exception):

using (var mock = AutoMock.GetLoose())
{
var mockClient = mock.Mock<CloudBlobClient>();
}

Cannot choose between multiple constructors with equal length 2 on type 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient'. Select the constructor explicitly, with the UsingConstructor() configuration method, when the component is registered.

当然,在我的单元测试中我没有注册任何东西,所以这条消息不是很有帮助。

我还尝试了其他方法,例如提供 NameParameters、TypedParameters,或调用 mock.Create 代替 mock.Mock,但我尝试的所有方法都返回相同的异常消息。

(在CloudBlobContainer上也出现了同样的问题)

实现接口(interface)后更新这是我编写的单元测试示例:

[TestMethod]
public void AzureBlobRepository_GetByIdAsync_ReturnsContent()
{
Guid blobId = Guid.NewGuid();
Guid directoryName = Guid.NewGuid();
string containerName = "unittest";

using (var mock = AutoMock.GetLoose())
{
var mockClient = mock.Mock<ICloudBlobClient>();
var mockContainer = mock.Mock<ICloudBlobContainer>();
var mockDirectory = mock.Mock<ICloudBlobDirectory>();
// notice that we're not using AutoMock here, it fails to create the mock
var mockBlob = new Mock<CloudBlockBlob>(new Uri($"http://tempuri.org/{containerName}/{directoryName}/{blobId}"));
mockBlob.Setup(m => m.DownloadTextAsync()).Returns(Task.FromResult("content"));

mockClient.Setup(m => m.GetContainerReference(containerName))
.Returns(mockContainer.Object);
mockContainer.Setup(m => m.GetDirectoryReference(directoryName.ToString()))
.Returns(mockDirectory.Object);
mockDirectory.Setup(m => m.GetBlockBlobReference(blobId.ToString()))
.Returns(mockBlob.Object);

var repository = mock.Create<AzureBlobRepository>(
new TypedParameter(typeof(ICloudBlobClient), mockClient.Object),
new NamedParameter("container", containerName),
new NamedParameter("directory", directoryName));

var result = repository.GetByIdAsync(blobId, directoryName).Result;
result.ShouldBe("content");
}
}

最佳答案

这些类应被视为第 3 方实现问题。这意味着您无法控制它们,我们不应该 mock 我们无法控制的东西。它们应该封装在您可以控制的抽象之后,并且可以在单独测试时根据需要进行模拟。

public interface ICloudBlobClient {
//...expose only the functionality I need
}

public class CloudBlobClientWrapper : ICloudBlobClient {
private readonly CloudBlobClient client;

public CloudBlobClientWrapper(CloudBlobClient client) {
this.client = client;
}

//...implement interface wrapping
}

正是出于这个原因,类应该依赖于抽象而不是具体化。模拟具体类往往会产生链式 react

包装器不需要完全包装客户端,但可以聚合功能以免暴露实现问题。

所以现在当单独测试时你可以模拟你控制的抽象

using (var mock = AutoMock.GetLoose()) {
var mockClient = mock.Mock<ICloudBlobClient>();

/// ...and the rest of the test.
}

关于c# - 使用 AutoFac 和 AutoMock 模拟 CloudBlobClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45876042/

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