Frinkware.Testing.WebApplicationFactory 1.1.0

dotnet add package Frinkware.Testing.WebApplicationFactory --version 1.1.0
                    
NuGet\Install-Package Frinkware.Testing.WebApplicationFactory -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Frinkware.Testing.WebApplicationFactory" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Frinkware.Testing.WebApplicationFactory" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Frinkware.Testing.WebApplicationFactory" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Frinkware.Testing.WebApplicationFactory --version 1.1.0
                    
#r "nuget: Frinkware.Testing.WebApplicationFactory, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Frinkware.Testing.WebApplicationFactory@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Frinkware.Testing.WebApplicationFactory&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Frinkware.Testing.WebApplicationFactory&version=1.1.0
                    
Install as a Cake Tool

Frinkware.Testing.WebApplicationFactory

A 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.

Features

  • Behavioral Test Context: Reuse a single WebApplicationFactory instance across multiple tests in a class
  • Mock Management: Built-in Moq integration with automatic behavioral reset between tests
  • Simple API: Create mocks and HTTP clients with minimal boilerplate
  • XUnit Integration: Works seamlessly as an XUnit class fixture

Installation

dotnet add package Frinkware.Testing.WebApplicationFactory

Quick Start

Basic Usage

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
    }
}

How It Works

BehavioralTestContext<TEntryPoint>

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.

Important Notes

  • Mock Early: All mocks must be created in your test class constructor, before calling CreateClient() for the first time
  • Automatic Reset: Mocks are automatically reset when accessed in subsequent tests, clearing all setups and verifications
  • One Factory: The same WebApplicationFactory instance is shared across all tests in the class for performance

Example: Testing a Weather API

public 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);
    }
}

Dependencies

  • .NET 8.0+
  • Microsoft.AspNetCore.Mvc.Testing 8.0.0+
  • Moq 4.20.70+
  • xunit.core 2.6.6+

License

MIT

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests at GitHub.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 103 1/14/2026
1.0.0 94 1/14/2026