liveBook · Manning

WebApplicationFactory

As explained in ASP.NET Core in Action, 3e

WebApplicationFactory is an ASP.NET Core testing API that bootstraps an application for in-memory functional and integration testing and permits test-specific customization.

WebApplicationFactory bootstraps an in-memory version of a real ASP.NET Core application for integration tests. It uses the application's configuration, dependency-injection services, and middleware pipeline, letting tests send HTTP requests and verify responses. section 36.3 section 36.3.2

A basic integration test follows these steps:

  • Add the Microsoft.AspNetCore.Mvc.Testing package. section 36.3.2
  • Implement IClassFixture<WebApplicationFactory<Program>> on the test class and inject the factory. section 36.3.2
  • Expose a public Program class when top-level statements make the generated class internal. section 36.3.2

Replacing an external dependency

When an application calls a third-party exchange-rate API, define and use an ICurrencyConverter interface. A test can replace the real implementation with StubCurrencyConverter through WithWebHostBuilder() and ConfigureTestServices(), then assert that the endpoint returns the controlled value. Repeated replacements can move into a custom factory that overrides ConfigureWebHost. section 36.3.3 section 36.3.4

Listing 36.8 Creating an integration test with WebApplicationFactory
public class IntegrationTests:                                     #1
    IClassFixture<WebApplicationFactory<Program>>                  #1
{
    private readonly WebApplicationFactory<Program> _fixture;      #2
    public IntegrationTests(                                       #2
        WebApplicationFactory<Startup> fixture)                    #2
    {                                                              #2
        _fixture = fixture;                                        #2
    }                                                              #2
 
    [Fact]
    public async Task PingRequest_ReturnsPong()
    {
        HttpClient client = _fixture.CreateClient();               #3
 
        var response = await client.GetAsync("/ping");             #4
 
        response.EnsureSuccessStatusCode();                        #4
        var content = await response.Content.ReadAsStringAsync();  #4
        Assert.Equal("pong", content);                             #4
    }
}

Use the complete listing to see fixture registration, factory injection, client creation, an in-memory request, and response verification together. section 36.3.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains setup, request verification, service replacement, and custom factories, but defers database-specific testing to a separate treatment.
Examples
Demonstrates GET /ping returning pong and a currency endpoint using a stub converter that returns 3.
What it assumes
Assumes a test project and ASP.NET Core application while teaching factory fixtures, HTTP clients, service interfaces, and test substitutions.
What you can do after
Apply it via Microsoft.AspNetCore.Mvc.Testing to verify endpoints and substitute test services without starting an external server.
How it connects
Connects integration testing with TestServer, dependency injection, middleware pipelines, and external-service substitution.

Also explained in

Compare how 2 books explain WebApplicationFactory →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.