Frinkware.Testing.WebApplicationFactory 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Frinkware.Testing.WebApplicationFactory --version 1.0.0
                    
NuGet\Install-Package Frinkware.Testing.WebApplicationFactory -Version 1.0.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.0.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.0.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.0.0
                    
#r "nuget: Frinkware.Testing.WebApplicationFactory, 1.0.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.0.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.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Frinkware.Testing.WebApplicationFactory&version=1.0.0
                    
Install as a Cake Tool

Frinkware.Testing.WebApplicationFactory

A NuGet package that provides a custom XUnit fixture class library for behavioral testing in ASP.NET Core applications. This library wraps Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory to simplify test setup, mock configuration, and test context reuse.

Features

  • Configurable Web Application Factory: Easily configure test services and command-line arguments
  • Behavioral Test Context Fixture: Reuse WebApplicationFactory across multiple tests in a class
  • Mock Management: Built-in support for Moq with behavioral reset capabilities
  • Service Replacement: Simple extension methods for replacing services with mocks
  • Hosted Service Control: Automatically filter out background services during tests
  • Default Value Provider: Set up default return values for mocked dependencies

Installation

dotnet add package Frinkware.Testing.WebApplicationFactory

Quick Start

Basic Usage

using Frinkware.Testing.WebApplicationFactory;
using Xunit;

public class MyApiTests : IClassFixture<BehavioralTestContextFixture<Startup>>
{
    private readonly HttpClient _httpClient;
    private readonly Mock<IMyService> _myServiceMock;

    public MyApiTests(BehavioralTestContextFixture<Startup> fixture)
    {
        // Mock out your dependencies
        _myServiceMock = fixture.Mock<IMyService>();
        _myServiceMock
            .Setup(x => x.GetDataAsync())
            .ReturnsAsync("test data");

        // Create an HTTP client
        _httpClient = fixture.CreateClient();
    }

    [Fact]
    public async Task MyEndpoint_ShouldReturnExpectedData()
    {
        // Act
        var response = await _httpClient.GetAsync("/api/myendpoint");

        // Assert
        response.EnsureSuccessStatusCode();
        var content = await response.Content.ReadAsStringAsync();
        Assert.Contains("test data", content);
    }
}

Using WithDefault for Simpler Mock Setup

public MyApiTests(BehavioralTestContextFixture<Startup> fixture)
{
    // Use WithDefault to automatically set return values for any method returning this type
    _myServiceMock = fixture.Mock<IMyService>()
        .WithDefault(() => myDefaultData);

    _httpClient = fixture.CreateClient();
}

Advanced Configuration

public class CustomFixture : BehavioralTestContextFixture<Startup>
{
    protected override Dictionary<string, object>? CommandLineArguments()
    {
        return new Dictionary<string, object>
        {
            { "ConnectionStrings:DefaultConnection", "test-connection-string" }
        };
    }
}

public class MyTests : IClassFixture<CustomFixture>
{
    // Your tests here
}

Key Components

ConfigurableWebApplicationFactory<TEntryPoint>

Extends Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory with:

  • Service collection configuration
  • Command-line argument injection
  • Hosted service filtering

BehavioralTestContextFixture<TEntryPoint>

XUnit class fixture that:

  • Shares a single WebApplicationFactory instance across tests
  • Manages mock lifecycle with behavioral reset
  • Provides easy client creation

Extension Methods

  • Mock<T>(): Create and register mocks with the test services
  • WithDefault<T, TReturn>(): Set default return values for mock methods
  • ResetBehavioral(): Reset mocks including custom default value providers
  • ReplaceAllWithSingleton<T>(): Replace all service registrations with a singleton

Dependencies

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

Credits

This library is based on the behavioral testing patterns from the BehavioralTesting repository by Jose-A-ProfessorFrink.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests. This project contains code to facilitate behavioral testing using Moq and the WebApplicationFactory.

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