How to use the Moq infrastructure for unit test of azure service fabrics?

I plan to use Moq to unit test my application with azure service. I saw some examples here https://github.com/Azure-Samples/service-fabric-dotnet-web-reference-app/blob/master/ReferenceApp/Inventory.UnitTests/InventoryServiceTests.cs . The testing I saw is like writing a reliable dictionary, not a mockery. Is there a way to mock adding / removing from a trusted dictionary? How to make unit test something like below

public async Task<bool> AddItem(MyItem item)
{
    var items = await StateManager.GetOrAddAsync<IReliableDictionary<int, MyItem>>("itemDict");

    using (ITransaction tx = this.StateManager.CreateTransaction())
    {
        await items.AddAsync(tx, item.Id, item);
        await tx.CommitAsync();
    }
    return true;
}
+4
source share
1 answer

DI , StateManager. , , IReliableStateManagerReplica

public class MyStatefulService : StatefulService 
{
    public MyStatefulService(StatefulServiceContext serviceContext, IReliableStateManagerReplica reliableStateManagerReplica)
        : base(serviceContext, reliableStateManagerReplica)
    {
    }
}

, (), IReliableStateManagerReplica

var reliableStateManagerReplica = new Mock<IReliableStateManagerReplica>();

var codePackageActivationContext = new Mock<ICodePackageActivationContext>();
var serviceContext = new StatefulServiceContext(new NodeContext("", new NodeId(8, 8), 8, "", ""), codePackageActivationContext.Object, string.Empty, new Uri("http://boo.net"), null, Guid.NewGuid(), 0L);

var myService = new MyService(serviceContext, reliableStateManagerReplica.Object);

reliableStateManagerReplica, .

var dictionary = new Mock<IReliableDictionary<int, MyItem>>();
reliableStateManagerReplica.Setup(m => m.GetOrAddAsync<IReliableDictionary<int, MyItem>>(name).Returns(Task.FromResult(dictionary.Object)); 

, .

: Moq.

+6

All Articles