ASP.NET Core MVC Interview Question #155
What is WebApplicationFactory in ASP.NET Core testing?
Testing, AJAX/JavaScript Integration, Localization & Advanced MVC Senior Advanced
Quick Interview Answer
WebApplicationFactory<TEntryPoint> creates a test host for an ASP.NET Core application and provides HttpClient instances that send requests through the application's pipeline. It is provided by the Microsoft.AspNetCore.Mvc.Testing package.
Detailed Explanation
WebApplicationFactory<TEntryPoint> creates a test host for an ASP.NET Core application and provides HttpClient instances that send requests through the application's pipeline. It is provided by the Microsoft.AspNetCore.Mvc.Testing package.
Tests can customize services and configuration for the test environment, such as replacing an external dependency or database. Test isolation remains important so one test does not unpredictably affect another.
Code Example
await using var factory =
new WebApplicationFactory<Program>();
var client = factory.CreateClient();
var response = await client.GetAsync("/Products");
response.EnsureSuccessStatusCode();