WithWebHostBuilder customizes a WebApplicationFactory before its in-memory TestServer starts. It returns a new factory, leaving the original fixture unchanged. section 36.3.3
Use it to adjust one integration test without changing the real application:
- Call ConfigureTestServices inside the host-builder callback. section 36.3.3
- Remove or replace registered dependencies, such as a third-party API client. section 36.5
- Create the client from the returned factory and send requests against the in-memory application. section 36.3.3
Replacing a currency converter for one test
A test can call _fixture.WithWebHostBuilder(builder => builder.ConfigureTestServices(services => { services.RemoveAll<ICurrencyConverter>(); services.AddScoped<ICurrencyConverter, StubCurrencyConverter>(); })); The returned factory uses the stub while other tests retain the original fixture configuration. section 36.3.3
