A custom XUnit fixture class library that wraps Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory to simplify behavioral testing in ASP.NET Core applications. Provides easy mock configuration, service replacement, and reusable test contexts.
$ dotnet add package Frinkware.Testing.WebApplicationFactoryA NuGet package that provides a simple XUnit fixture class for behavioral testing in ASP.NET Core applications. This library wraps Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory to simplify test setup with Moq-based mocking and reusable test contexts.
dotnet add package Frinkware.Testing.WebApplicationFactory
using Frinkware.Testing.WebApplicationFactory;
using Xunit;
public class MyApiTests : IClassFixture<BehavioralTestContext<Program>>
{
private readonly BehavioralTestContext<Program> _context;
private readonly Mock<IMyService> _myServiceMock;
public MyApiTests(BehavioralTestContext<Program> context)
{
_context = context;
// Mock out your dependencies
_myServiceMock = context.Mock<IMyService>();
_myServiceMock
.Setup(x => x.GetDataAsync())
.ReturnsAsync("test data");
}
[Fact]
public async Task MyEndpoint_ShouldReturnExpectedData()
{
// Arrange
var client = _context.CreateClient();
// Act
var response = await client.GetAsync("/api/myendpoint");
// Assert
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
Assert.Contains("test data", content);
}
[Fact]
public async Task AnotherTest_MocksAreReset()
{
// The mock is automatically reset between tests
// You can set up new behavior for this test
_myServiceMock
.Setup(x => x.GetDataAsync())
.ReturnsAsync("different data");
var client = _context.CreateClient();
// ... test continues
}
}
The BehavioralTestContext<TEntryPoint> class is the main public API:
Mock<T>(MockBehavior behavior = MockBehavior.Default): Creates a mock and registers it with the test application's dependency injection container. Mocks are automatically reset between tests.
CreateClient(): Creates an HttpClient configured to call your test application.
CreateClient(WebApplicationFactoryClientOptions options): Creates an HttpClient with custom options.
CreateClient() for the first timepublic class WeatherForecastTests : IClassFixture<BehavioralTestContext<Program>>
{
private readonly BehavioralTestContext<Program> _context;
private readonly Mock<IWeatherService> _weatherServiceMock;
public WeatherForecastTests(BehavioralTestContext<Program> context)
{
_context = context;
_weatherServiceMock = context.Mock<IWeatherService>();
}
[Fact]
public async Task GetWeather_ReturnsSunny()
{
// Arrange
_weatherServiceMock
.Setup(x => x.GetForecastAsync())
.ReturnsAsync(new[] { new WeatherForecast { Summary = "Sunny" } });
var client = _context.CreateClient();
// Act
var response = await client.GetAsync("/weatherforecast");
// Assert
response.EnsureSuccessStatusCode();
var forecast = await response.Content.ReadFromJsonAsync<WeatherForecast[]>();
Assert.Equal("Sunny", forecast[0].Summary);
}
}
MIT
Contributions are welcome! Please feel free to submit issues or pull requests at GitHub.