Table of Contents

ManagedHostFixture Class

Definition

Namespace
Codebelt.Extensions.Xunit.Hosting
Assemblies
Codebelt.Extensions.Xunit.Hosting.dll
Source
src/Codebelt.Extensions.Xunit.Hosting/ManagedHostFixture.cs

Provides a default implementation of the IGenericHostFixture interface.

public class ManagedHostFixture : HostFixture, IAsyncLifetime, IGenericHostFixture, IHostFixture, IConfigurationTest, IEnvironmentTest, IDisposable, IAsyncDisposable
Inheritance
ManagedHostFixture
Implements
IAsyncLifetime
Derived
Inherited Members
Extension Methods

Examples

The following example uses ManagedHostFixture as the fixture type for a generic host test class. The fixture owns the full IHost lifecycle, starting the host before the test runs and disposing it afterward. This is the standard pattern for testing services that depend on dependency injection and need a running host.

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

namespace HostFixtureTests;

public class BackgroundServiceTest : HostTest<ManagedHostFixture>
{
    public BackgroundServiceTest(ManagedHostFixture hostFixture, ITestOutputHelper output) : base(hostFixture, output)
    {
    }

    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddHostedService<TestWorker>();
        services.AddXunitTestLogging();
    }

    [Fact]
    public void Host_ShouldBeRunning()
    {
        Assert.NotNull(Host);
    }
}

public class TestWorker : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            await Task.Delay(100, stoppingToken);
        }
    }
}

Constructors

Name Description
ManagedHostFixture()

Initializes a new instance of the ManagedHostFixture class.

Properties

Name Description
ConfigureHostCallback

Gets or sets the delegate that initializes the host builder.

ConfigureServicesCallback

Gets or sets the delegate that adds services to the container.

Methods

Name Description
ConfigureHost(Test)

Creates and configures the IHost of this instance.

See Also