Table of Contents

AppClientFixture

Assembly: FEFF.TestFixtures.AspNetCore.dll
Namespace: FEFF.TestFixtures.AspNetCore
Source: AppClientFixture.cs

Overview

The AppClientFixture is an extension to AppManagerFixture.
It automates the creation and disposal of an HttpClient connected to the test application.

Key Features

  • Lazy Initialization: The application starts only when LazyValue is first accessed
  • Automatic HttpClient Lifecycle Management: AppClientFixture leverages the fixture disposal mechanism
  • Can be used instead of AppManagerFixture: AppClientFixture internally requests AppManagerFixture as a dependency to access the test application, providing a higher-level abstraction with a ready-to-use HttpClient

Basic Usage

First, define your test application entry point:

// ASP.NET Core test application entry point (Program.cs)
public class Program
{
    public record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary);

    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        var app = builder.Build();

        app.MapGet("/const", () =>
        {
            return new WeatherForecast(DateOnly.Parse("2000-01-01"), 20, "normal");
        });

        app.Run();
    }
}

Then use the fixture in your tests:

using FEFF.TestFixtures.AspNetCore;

public class ApiTests
{
    protected IAppClientFixture ClientFx { get; } = 
        TestContext.Current.GetFeffFixture<AppClientFixture<Program>>();

    protected HttpClient Client => ClientFx.LazyValue;

    [Fact]
    public async Task Example1__Client__should__get_response()
    {
        var resp = await Client.GetAsync("/const", TestContext.Current.CancellationToken);
        var body = await resp.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
        resp.StatusCode.Should().Be(HttpStatusCode.OK, body);

        JToken.Parse(body)
            .Should().BeEquivalentTo(
            """
            {
                "date": "2000-01-01",
                "temperatureC": 20,
                "summary": "normal"
            }
            """);
    }
}

Note: in this example, access to the HttpClient is:

protected HttpClient Client => ClientFx.LazyValue;

This differs from direct usage of AppManagerFixture:

using var client = AppManagerFx.LazyApplication.CreateClient();

Type Arguments

Type Argument Constraint Description
TEntryPoint class The application entry point type. Typically the Startup or Program class.

Key Members

Member Type Description
LazyValue HttpClient Gets the lazily-created HttpClient. Starts the application under test on first access if not already running.

See Also

Link Description
API: AppClientFixture API reference
AppClientFixtureTests.cs Unit tests for AppClientFixture
API Integration Example Integration test examples using AppClientFixture