how to unit test asp.net core application with constructor dependency injection

后端 未结 5 1315
难免孤独
难免孤独 2020-12-07 10:43

I have a asp.net core application that uses dependency injection defined in the startup.cs class of the application:

    public void ConfigureServices(ISer         


        
5条回答
  •  佛祖请我去吃肉
    2020-12-07 10:51

    Although @Kritner's answer is correct, I prefer the following for code integrity and better DI experience:

    [TestClass]
    public class MatchRepositoryTests
    {
        private readonly IMatchRepository matchRepository;
    
        public MatchRepositoryTests()
        {
            var services = new ServiceCollection();
            services.AddTransient();
    
            var serviceProvider = services.BuildServiceProvider();
    
            matchRepository = serviceProvider.GetService();
        }
    }
    

提交回复
热议问题