Moq IServiceProvider / IServiceScope

前端 未结 5 1553
挽巷
挽巷 2021-02-01 14:01

I am trying to create a Mock (using Moq) for an IServiceProvider so that I can test my repository class:

public class ApiResourceRepository : IApiRe         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 15:00

    As already stated, Moq does not allow setup of extension methods.

    In this case however the source code of the said extension methods are available on Github

    ServiceProviderServiceExtensions.

    The usual way around an issue like this is to find out what the extension methods do and mock a path safely through it's execution.

    The base type in all of this is the IServiceProvider and its object Getservice(Type type) method. This method is what is ultimately called when resolving the service type. And we are only dealing with abstraction (interfaces) then that makes using moq all the more easier.

    //Arrange
    var serviceProvider = new Mock();
    serviceProvider
        .Setup(x => x.GetService(typeof(ConfigurationDbContext)))
        .Returns(new ConfigurationDbContext(Options, StoreOptions));
    
    var serviceScope = new Mock();
    serviceScope.Setup(x => x.ServiceProvider).Returns(serviceProvider.Object);
    
    var serviceScopeFactory = new Mock();
    serviceScopeFactory
        .Setup(x => x.CreateScope())
        .Returns(serviceScope.Object);
    
    serviceProvider
        .Setup(x => x.GetService(typeof(IServiceScopeFactory)))
        .Returns(serviceScopeFactory.Object);
    
    var sut = new ApiResourceRepository(serviceProvider.Object);
    
    //Act
    var actual = sut.Get(myIntValue);
    
    //Asssert
    //...
    

    Review the code above and you would see how the arrangement satisfies the expected behavior of the extension methods and by extension (no pun intended) the method under test.

提交回复
热议问题