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

后端 未结 5 1306
难免孤独
难免孤独 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 11:01

    Why would you want to inject those in a test class? You would usually test the MatchController, for example, by using a tool like RhinoMocks to create stubs or mocks. Here's an example using that and MSTest, from which you can extrapolate:

    [TestClass]
    public class MatchControllerTests
    {
        private readonly MatchController _sut;
        private readonly IMatchService _matchService;
    
        public MatchControllerTests()
        {
            _matchService = MockRepository.GenerateMock();
            _sut = new ProductController(_matchService);
        }
    
        [TestMethod]
        public void DoSomething_WithCertainParameters_ShouldDoSomething()
        {
            _matchService
                   .Expect(x => x.GetMatches(Arg.Is.Anything))
                   .Return(new []{new Match()});
    
            _sut.DoSomething();
    
            _matchService.AssertWasCalled(x => x.GetMatches(Arg.Is.Anything);
        }
    

提交回复
热议问题