Table of Contents

Class BlockingManagedApplicationFixture<TEntryPoint>

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

Provides a blocking managed implementation of the IApplicationFixture<TEntryPoint> interface.

public class BlockingManagedApplicationFixture<TEntryPoint> : HostFixture, IAsyncLifetime, IApplicationFixture<TEntryPoint>, IHostFixture, IConfigurationTest, IEnvironmentTest, IDisposable, IAsyncDisposable where TEntryPoint : class

Type Parameters

TEntryPoint

A type in the entry point assembly of the application.

Inheritance
BlockingManagedApplicationFixture<TEntryPoint>
Implements
IAsyncLifetime
IApplicationFixture<TEntryPoint>
Inherited Members
Extension Methods

Examples

The test project references a worker application's entry point and shares one bootstrapped host through xUnit's class-fixture lifetime. BlockingManagedApplicationFixture<TEntryPoint> waits until the host is ready before constructing the test class, so each test can immediately resolve services registered by the real application.

using Codebelt.Extensions.Xunit.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;

namespace InventoryWorker.Tests;

public sealed class InventoryWorkerTest : IClassFixture<BlockingManagedApplicationFixture<WorkerProgram>>
{
    private readonly BlockingManagedApplicationFixture<WorkerProgram> _fixture;

    public InventoryWorkerTest(BlockingManagedApplicationFixture<WorkerProgram> fixture)
    {
        _fixture = fixture;
    }

    [Fact]
    public void Host_ContainsApplicationService()
    {
        var identity = _fixture.Host.Services.GetRequiredService<WorkerIdentity>();

        Assert.Equal("Inventory worker", identity.Name);
    }
}

public sealed record WorkerIdentity(string Name);

public sealed class WorkerProgram
{
    public static void Main(string[] args)
    {
        var builder = Host.CreateApplicationBuilder(args);
        builder.Services.AddSingleton(new WorkerIdentity("Inventory worker"));

        using var host = builder.Build();
        host.Run();
    }
}

Remarks

Unlike the base managed host fixtures, this fixture starts the resolved application host synchronously. Application entry point testing must expose a fully started host after fixture initialization.

Constructors

BlockingManagedApplicationFixture()

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

public BlockingManagedApplicationFixture()

Properties

ConfigureHostCallback

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

public Action<IHostBuilder> ConfigureHostCallback { get; set; }

Property Value

Action<IHostBuilder>

The delegate that provides a way to override the IHostBuilder.

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 ApplicationTest<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 ApplicationTest<TEntryPoint, T>.

See Also