Class HostTestFactory
- Namespace
- Codebelt.Extensions.Xunit.Hosting
- Assembly
- Codebelt.Extensions.Xunit.Hosting.dll
Provides a set of static methods for IHost unit testing.
public static class HostTestFactory
- Inheritance
-
HostTestFactory
Examples
Use HostTestFactory when the test owns the Generic Host setup instead of loading an existing application entry point. This example registers a worker-facing status service, creates a managed host context, and reads the configured value through the same service provider the hosted application would use.
using Codebelt.Extensions.Xunit.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace WorkerServices.Tests;
public sealed class HostTestFactoryExample
{
public string ResolveWorkerStatus()
{
using var host = HostTestFactory.Create(
services => services.AddSingleton(new WorkerStatus("ready")));
var status = host.Host.Services.GetRequiredService<WorkerStatus>();
return status.Value;
}
}
public sealed record WorkerStatus(string Value);
Methods
Create(Action<IServiceCollection>, Action<IHostBuilder>, IGenericHostFixture)
Creates and returns an IHostTest implementation.
public static IHostTest Create(Action<IServiceCollection> serviceSetup = null, Action<IHostBuilder> hostSetup = null, IGenericHostFixture hostFixture = null)
Parameters
serviceSetupAction<IServiceCollection>The IServiceCollection which may be configured.
hostSetupAction<IHostBuilder>The IHostBuilder which may be configured.
hostFixtureIGenericHostFixtureAn optional IGenericHostFixture implementation to use instead of the default ManagedHostFixture instance.
Returns
CreateWithHostBuilderContext(Action<HostBuilderContext, IServiceCollection>, Action<IHostBuilder>, IGenericHostFixture)
Creates and returns an IHostTest implementation.
public static IHostTest CreateWithHostBuilderContext(Action<HostBuilderContext, IServiceCollection> serviceSetup = null, Action<IHostBuilder> hostSetup = null, IGenericHostFixture hostFixture = null)
Parameters
serviceSetupAction<HostBuilderContext, IServiceCollection>The IServiceCollection which may be configured.
hostSetupAction<IHostBuilder>The IHostBuilder which may be configured.
hostFixtureIGenericHostFixtureAn optional IGenericHostFixture implementation to use instead of the default ManagedHostFixture instance.