Table of Contents

Class ManagedHostFixture

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

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

ManagedHostFixture()

Initializes a new instance of the ManagedHostFixture class.

public ManagedHostFixture()

Properties

ConfigureHostCallback

Gets or sets the delegate that initializes the host builder.

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

Property Value

Action<IHostBuilder>

The delegate that initializes the host builder.

ConfigureServicesCallback

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

public Action<IServiceCollection> ConfigureServicesCallback { get; set; }

Property Value

Action<IServiceCollection>

The delegate that adds services to the container.

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 HostTest<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 HostTest<T>.

See Also