Mocking Azure BlobItem - C# unit test
Asked Answered
M

1

16

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?

Melany answered 31/3, 2021 at 20:26 Comment(0)
M
33

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")
Melany answered 2/4, 2021 at 0:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.