Reconfigure dependencies when Integration testing ASP.NET Core Web API and EF Core

@ilya-chumakov’s answer is awesome. I just would like to add one more option

3. Use ConfigureTestServices method from WebHostBuilderExtensions.

The method ConfigureTestServices is available in the Microsoft.AspNetCore.TestHost version 2.1(on 20.05.2018 it is RC1-final). And it lets us override existing registrations with mocks.

The code:

_server = new TestServer(new WebHostBuilder()
    .UseStartup<Startup>()
    .ConfigureTestServices(services =>
    {
        services.AddTransient<IFooService, MockService>();
    })
);

Leave a Comment