简体   繁体   English

Mocking Azure BlobItem - C# 单元测试

[英]Mocking Azure BlobItem - C# unit test

I have some unit tests where I want to mock the Azure BlobItem class (Azure.Storage.Blobs.Models).我有一些单元测试,我想在其中模拟 Azure BlobItem class (Azure.Storage.Blobs.Models)。

Here is my test class I'm using.这是我正在使用的测试 class。 I want the Values getter to return a list with a mock BlobItem.我希望Values getter 返回一个带有模拟 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();
    }
}

However when I do this, the Name property on my mock BlobItem is always null.但是,当我这样做时,我的模拟 BlobItem 上的Name属性始终是 null。

I can try to set it up so that Name would return a mocked value like so:我可以尝试设置它,以便Name会返回一个模拟值,如下所示:

var mock = new Mock<BlobItem>();
mock.SetupGet(x => x.Name).Returns("mock");

But then I get an error:但后来我得到一个错误:

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.但我不知道解决方案是什么。

I want a mocked BlobItem to return a non-null Name value.我想要一个模拟的 BlobItem 返回一个非空的Name值。 How can I achieve this?我怎样才能做到这一点?

Apparently there's a factory class called BlobsModelFactory .显然有一个名为BlobsModelFactory

You can mock a BlobItem with whatever name you want by calling:您可以通过调用以任何您想要的名称模拟 BlobItem:

var blob = BlobsModelFactory.BlobItem("mocked.mocked.mocked.mocked")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM