ASP.NET Core Web API Interview Question #154
What is WebApplicationFactory in ASP.NET Core testing?
Testing, HttpClient, Integration Testing, Background Services & Deployment Senior Advanced
Quick Interview Answer
WebApplicationFactory<TEntryPoint> creates a test server for an ASP.NET Core application and provides HttpClient instances that call the application in memory. It is commonly used for end-to-end integration tests of the HTTP pipeline.
Detailed Explanation
WebApplicationFactory<TEntryPoint> creates a test server for an ASP.NET Core application and provides HttpClient instances that call the application in memory. It is commonly used for end-to-end integration tests of the HTTP pipeline.
The factory can replace services, configuration, authentication, or database registrations specifically for the test environment.
Code Example
public class ApiTests :
IClassFixture<WebApplicationFactory<Program>>
{
private readonly HttpClient _client;
public ApiTests(WebApplicationFactory<Program> factory)
{
_client = factory.CreateClient();
}
}