Class SelfManagedHostFixture
- Namespace
- Codebelt.Extensions.Xunit.Hosting
- Assembly
- Codebelt.Extensions.Xunit.Hosting.dll
Represents a self-managed implementation of the ManagedHostFixture class.
public sealed class SelfManagedHostFixture : ManagedHostFixture, IAsyncLifetime, IGenericHostFixture, IHostFixture, IConfigurationTest, IEnvironmentTest, IDisposable, IAsyncDisposable
- Inheritance
-
SelfManagedHostFixture
- Implements
-
IAsyncLifetime
- Inherited Members
- Extension Methods
Examples
Use SelfManagedHostFixture when the host must be built before it is started. The example attaches an ApplicationStarted observer first, verifies that factory creation did not start the host, and then starts it explicitly so the lifecycle transition is under test control.
using System.Threading.Tasks;
using Codebelt.Extensions.Xunit.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
namespace WorkerLifecycle.Tests;
public sealed class SelfManagedHostFixtureExample
{
public async Task StartAfterAttachingObserverAsync()
{
using var host = HostTestFactory.Create(
hostFixture: new SelfManagedHostFixture());
var started = false;
var lifetime = host.Host.Services.GetRequiredService<IHostApplicationLifetime>();
using var registration = lifetime.ApplicationStarted.Register(() => started = true);
Assert.False(started);
await host.Host.StartAsync().ConfigureAwait(false);
Assert.True(started);
}
}
Constructors
SelfManagedHostFixture()
Initializes a new instance of the SelfManagedHostFixture class.
public SelfManagedHostFixture()
Remarks
This constructor sets the AsyncHostRunnerCallback to a no-op asynchronous delegate.