Table of Contents

Class ManagedWebApplicationFixture<TEntryPoint>

Namespace
Codebelt.Extensions.Xunit.Hosting.AspNetCore
Assembly
Codebelt.Extensions.Xunit.Hosting.AspNetCore.dll

Provides an entrypoint-owned implementation of the IWebApplicationFixture<TEntryPoint> interface.

public class ManagedWebApplicationFixture<TEntryPoint> : HostFixture, IAsyncLifetime, IWebApplicationFixture<TEntryPoint>, IHostFixture, IConfigurationTest, IEnvironmentTest, IDisposable, IAsyncDisposable where TEntryPoint : class

Type Parameters

TEntryPoint

A type in the entry point assembly of the application.

Inheritance
ManagedWebApplicationFixture<TEntryPoint>
Implements
IAsyncLifetime
Inherited Members
Extension Methods

Examples

Use ManagedWebApplicationFixture<TEntryPoint> when an xUnit class fixture should exercise an ASP.NET Core application's real entry point and let that entry point start the in-memory server. Derive the test from WebApplicationTest<TEntryPoint,T> and pass the fixture to its base constructor so the base class initializes the fixture through ConfigureHost before the test reads Server. Fixture setup remains lazy; consuming the test host starts the deferred host, after which the test can create a client from the exposed TestServer and verify the application's endpoint behavior. WebApplicationTestFactory uses this fixture by default.

using System.Threading.Tasks;
using Codebelt.Extensions.Xunit.Hosting.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace CatalogApi.Tests;

public sealed class CatalogApiTest : WebApplicationTest<CatalogProgram, ManagedWebApplicationFixture<CatalogProgram>>
{
    public CatalogApiTest(ManagedWebApplicationFixture<CatalogProgram> fixture, ITestOutputHelper output)
        : base(fixture, output)
    {
    }

    [Fact]
    public async Task HealthEndpoint_ReturnsApplicationState()
    {
        using var client = Server.CreateClient();

        var body = await client.GetStringAsync("/health").ConfigureAwait(false);

        Assert.Equal("ready", body);
    }
}

public sealed record CatalogStatus(string Value);

public sealed class CatalogProgram
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        builder.Services.AddSingleton(new CatalogStatus("ready"));

        var app = builder.Build();
        app.MapGet("/health", (CatalogStatus status) => status.Value);
        app.Run();
    }
}

Remarks

The ASP.NET Core application's Main method owns host startup. The fixture captures the host after it has been built without starting it during fixture setup; the test host starts the deferred host when it is consumed.

Constructors

ManagedWebApplicationFixture()

Initializes a new instance of the ManagedWebApplicationFixture<TEntryPoint> class.

public ManagedWebApplicationFixture()

Properties

ConfigureWebHostCallback

Gets or sets the delegate that provides a way to override the IWebHostBuilder before the application is built.

public Action<IWebHostBuilder> ConfigureWebHostCallback { get; set; }

Property Value

Action<IWebHostBuilder>

The delegate that provides a way to override the IWebHostBuilder.

Server

Gets the TestServer initialized by this instance.

public TestServer Server { get; protected set; }

Property Value

TestServer

The TestServer initialized by this instance.

Methods

ConfigureHost(Test)

Creates and configures the IHost of this instance.

public virtual void ConfigureHost(Test hostTest)

Parameters

hostTest Test

The object that inherits from WebApplicationTest<TEntryPoint, T>.

Remarks

hostTest was added to support those cases where the caller is required in the host configuration.

Exceptions

ArgumentNullException

hostTest is null.

ArgumentOutOfRangeException

hostTest is not assignable from WebApplicationTest<TEntryPoint, T>.

See Also