Moq IServiceProvider / IServiceScope

前端 未结 5 1544
挽巷
挽巷 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 14:50

    DISCLAIMER: The embedded links point to subpages of my GitHub and NuGet Page. But I hope it helps you or someone else, never the less.


    I just created exaclty such a thing because I couldn't find any. It implements IServiceCollection and IServiceProvider to test my Startup-Configuration, especially, whether all types are registered properly to the DI-Container. And it is a general purpose replacement for those interfaces, providing Mocks (Moq) as singletons for each Registered Type. Foo is different from Foo.

    There is a readme.md on GitHub and the code base is not that big.

    There is also a nuget package called MockProvider and - as mentioned - the code is on GitHub. I put it under MIT, so do what you want with it. It is free to use and contribute.

    Consider it a way of giving back.

提交回复
热议问题