The goal of integration testing in the MVC project is to verify the stack behaves as expected, ensuring endpoints are correctly routed, requests are validated and the correct response types and codes are returned.
The underlying implementations called by the endpoints are stubbed to simplify the tests and ensure they are performant. This approach is often referred to as narrow integration testing.
Narrow integration testing ensures the stack is wired up correctly prior to running slower end-to-end tests that exercise the full stack, including underlying implementations.
While targeted at MVC sites, the Integration Testing in ASP.NET Core guide provides a good overview of the approach used in this project.
Rather than starting a web server and sending HTTP requests from a separate process the tests leverage the WebApplicationFactory to host the MVC in-process by extending the MVC's Program.cs.
This allows the tests to send HTTP requests directly to the controller without the overhead of starting a separate process allowing tests run faster in an isolated fashion.
The WebApplicationFactory supports narrow integration testing by allowing injected services to be replaced with mocks using the ConfigureTestServices method while building the factory. The below snippet demonstrates replacing the IQuoteService with a test implementation TestQuoteService during test setup.
var client = _factory.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
services.AddScoped<IQuoteService, TestQuoteService>();
});
})
.CreateClient();For more advanced scenarios a Custom WebApplicationFactory may be created to provide common defaults across tests and reduce boilerplate setup.
The IntegrationTests project contains 'narrow' integration tests for the controller endpoints.
As with unit tests, the integrations tests abide by the suggested testing standards.