简体   繁体   中英

Mocking Azure BlobItem - C# unit test

I have some unit tests where I want to mock the Azure BlobItem class (Azure.Storage.Blobs.Models).

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();
    }
}

However when I do this, the Name property on my mock BlobItem is always null.

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");

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. How can I achieve this?

Apparently there's a factory class called BlobsModelFactory .

You can mock a BlobItem with whatever name you want by calling:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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