Table of Contents

MinimalHostTestFactory Class

Definition

Namespace
Codebelt.Extensions.Xunit.Hosting
Assemblies
Codebelt.Extensions.Xunit.Hosting.dll
Source
src/Codebelt.Extensions.Xunit.Hosting/MinimalHostTestFactory.cs

Provides a set of static methods for IHost unit testing (minimal style).

public static class MinimalHostTestFactory
Inheritance
MinimalHostTestFactory

Examples

The following example uses MinimalHostTestFactory to create a lightweight host context for testing a simple service. Unlike the generic host factory, the minimal host avoids the full IHostBuilder pipeline and is suitable for isolated unit tests that only need dependency injection and logging.

using System;
using Codebelt.Extensions.Xunit.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace HostFixtureTests;

public class MinimalHostFactoryExample
{
    public void Demonstrate()
    {
        using var host = MinimalHostTestFactory.Create(
            services => services.AddSingleton(new Version(1, 0, 0)));
        var version = host.Host.Services.GetRequiredService<Version>();
        Console.WriteLine(version);
    }
}

Methods

Name Description
Create(Action<IServiceCollection>, Action<IHostApplicationBuilder>, IMinimalHostFixture)

Creates and returns an IHostTest implementation.

CreateWithHostBuilderContext(Action<HostBuilderContext, IServiceCollection>, Action<IHostApplicationBuilder>, IMinimalHostFixture)

Creates and returns an IHostTest implementation.