模拟 Azure BlobItem - C# 单元测试 [英] Mocking Azure BlobItem - C# unit test
问题描述
我有一些单元测试,我想模拟 Azure BlobItem
类 (Azure.Storage.Blobs.Models).
I have some unit tests where I want to mock the Azure BlobItem
class (Azure.Storage.Blobs.Models).
这是我正在使用的测试类.我希望 Values
获取器返回一个带有模拟 BlobItem 的列表.这是有效的.
Here is my test class I'm using. I want the Values
getter to return a list with a mock BlobItem. That is working.
public class TestPage : Page<BlobItem>
{
public override IReadOnlyList<BlobItem> Values
{
get
{
return new List<BlobItem>() { new Mock<BlobItem>().Object };
}
}
public override string ContinuationToken => throw new NotImplementedException();
public override Response GetRawResponse()
{
throw new NotImplementedException();
}
}
但是,当我这样做时,我的模拟 BlobItem 上的 Name
属性始终为空.
However when I do this, the Name
property on my mock BlobItem is always null.
我可以尝试设置它,以便 Name
返回一个模拟值,如下所示:
I can try to set it up so that Name
would return a mocked value like so:
var mock = new Mock<BlobItem>();
mock.SetupGet(x => x.Name).Returns("mock");
但是我得到一个错误:
Non-overridable members may not be used in setup/verification expressions.
我明白为什么会发生这个错误.但我不知道解决方案是什么.
I understand why this error is happening. But I do not know what the solution is.
我想要一个模拟的 BlobItem 返回一个非空的 Name
值.我怎样才能做到这一点?
I want a mocked BlobItem to return a non-null Name
value. How can I achieve this?
推荐答案
显然有一个名为 BlobsModelFactory
的工厂类.
Apparently there's a factory class called BlobsModelFactory
.
您可以通过调用以下任何名称来模拟 BlobItem:
You can mock a BlobItem with whatever name you want by calling:
var blob = BlobsModelFactory.BlobItem("mocked.mocked.mocked.mocked")
这篇关于模拟 Azure BlobItem - C# 单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!