Frinkware.Testing.WebApplicationFactory
1.0.0
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
<PackageReference Include="Frinkware.Testing.WebApplicationFactory" Version="1.0.0" />
<PackageVersion Include="Frinkware.Testing.WebApplicationFactory" Version="1.0.0" />
<PackageReference Include="Frinkware.Testing.WebApplicationFactory" />
paket add Frinkware.Testing.WebApplicationFactory --version 1.0.0
#r "nuget: Frinkware.Testing.WebApplicationFactory, 1.0.0"
#:package Frinkware.Testing.WebApplicationFactory@1.0.0
#addin nuget:?package=Frinkware.Testing.WebApplicationFactory&version=1.0.0
#tool nuget:?package=Frinkware.Testing.WebApplicationFactory&version=1.0.0
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 servicesWithDefault<T, TReturn>(): Set default return values for mock methodsResetBehavioral(): Reset mocks including custom default value providersReplaceAllWithSingleton<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 | Versions 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. |
-
net8.0
- Microsoft.AspNetCore.Mvc.Testing (>= 8.0.0)
- Moq (>= 4.20.70)
- xunit.core (>= 2.6.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.