Class ManagedApplicationFixture<TEntryPoint>
- Namespace
- Codebelt.Extensions.Xunit.Hosting
- Assembly
- Codebelt.Extensions.Xunit.Hosting.dll
Provides an entrypoint-owned implementation of the IApplicationFixture<TEntryPoint> interface.
public class ManagedApplicationFixture<TEntryPoint> : HostFixture, IAsyncLifetime, IApplicationFixture<TEntryPoint>, IHostFixture, IConfigurationTest, IEnvironmentTest, IDisposable, IAsyncDisposable where TEntryPoint : class
Type Parameters
TEntryPointA type in the entry point assembly of the application.
- Inheritance
-
ManagedApplicationFixture<TEntryPoint>
- Implements
-
IAsyncLifetimeIApplicationFixture<TEntryPoint>
- Inherited Members
- Extension Methods
Examples
Use ManagedApplicationFixture<TEntryPoint> when an xUnit class fixture should exercise the application's real entry point and let that entry point start the host. Derive the test from ApplicationTest<TEntryPoint,T> and pass the fixture to its base constructor so the base class initializes the fixture through ConfigureHost before the test reads Host. Fixture setup remains lazy; accessing the test host starts the deferred host and surfaces startup failures at the point the test consumes it. ApplicationTestFactory uses this fixture by default.
using Codebelt.Extensions.Xunit.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
namespace InventoryWorker.Tests;
public sealed class InventoryWorkerTest : ApplicationTest<WorkerProgram, ManagedApplicationFixture<WorkerProgram>>
{
public InventoryWorkerTest(ManagedApplicationFixture<WorkerProgram> fixture, ITestOutputHelper output)
: base(fixture, output)
{
}
[Fact]
public void Host_ContainsApplicationService()
{
var identity = 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
The 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
ManagedApplicationFixture()
Initializes a new instance of the ManagedApplicationFixture<TEntryPoint> class.
public ManagedApplicationFixture()
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
Remarks
hostTest was added to support those cases where the caller is required in the host configuration.
Exceptions
- ArgumentNullException
hostTestis null.- ArgumentOutOfRangeException
hostTestis not assignable from HostTest.