Class ApplicationTestFactory
- Namespace
- Codebelt.Extensions.Xunit.Hosting
- Assembly
- Codebelt.Extensions.Xunit.Hosting.dll
Provides a set of static methods for IHost testing that bootstraps an existing .NET application entry point.
public static class ApplicationTestFactory
- Inheritance
-
ApplicationTestFactory
Examples
The test project references a worker application whose entry point registers WorkerIdentity. ApplicationTestFactory runs that application's real host setup, then exposes its services and environment through an owned test context so the test can verify application behavior without recreating Program configuration.
using Codebelt.Extensions.Xunit.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
namespace WorkerApp.Tests;
public sealed class ApplicationTestFactoryExample
{
[Fact]
public void Create_BootstrapsWorkerApplicationServices()
{
using var application = ApplicationTestFactory.Create<WorkerProgram>();
var identity = application.Host.Services.GetRequiredService<WorkerIdentity>();
Assert.Equal("Inventory worker", identity.Name);
Assert.Equal(Environments.Development, application.Environment.EnvironmentName);
}
}
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();
}
}
Methods
Create<TEntryPoint>(Action<IHostBuilder>, IApplicationFixture<TEntryPoint>)
Creates and returns an IHostTest implementation.
public static IHostTest Create<TEntryPoint>(Action<IHostBuilder> hostSetup = null, IApplicationFixture<TEntryPoint> hostFixture = null) where TEntryPoint : class
Parameters
hostSetupAction<IHostBuilder>The IHostBuilder which may be configured.
hostFixtureIApplicationFixture<TEntryPoint>An optional IApplicationFixture<TEntryPoint> implementation to use instead of the default BlockingManagedApplicationFixture<TEntryPoint> instance.
Returns
Type Parameters
TEntryPointA type in the entry point assembly of the application.