Table of Contents

Class WebHostTestFactory

Namespace
Codebelt.Extensions.Xunit.Hosting.AspNetCore
Assembly
Codebelt.Extensions.Xunit.Hosting.AspNetCore.dll

Provides a set of static methods for ASP.NET Core (including, but not limited to MVC, Razor and related) unit testing.

public static class WebHostTestFactory
Inheritance
WebHostTestFactory

Examples

Use WebHostTestFactory when the test defines a conventional ASP.NET Core service collection and middleware pipeline instead of bootstrapping an application entry point. RunAsync starts that pipeline on TestServer, sends a request, and returns the response so the caller can verify middleware behavior.

using System.Threading.Tasks;
using Codebelt.Extensions.Xunit.Hosting.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace RequestPipeline.Tests;

public sealed class WebHostTestFactoryExample
{
    public async Task<string> InvokeStatusMiddlewareAsync()
    {
        using var response = await WebHostTestFactory.RunAsync(
            services => services.AddSingleton(new PipelineStatus("ready")),
            app => app.Run(context =>
            {
                var status = context.RequestServices.GetRequiredService<PipelineStatus>();
                return context.Response.WriteAsync(status.Value);
            })).ConfigureAwait(false);

        return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
    }
}

public sealed record PipelineStatus(string Value);

Methods

Create(Action<IServiceCollection>, Action<IApplicationBuilder>, Action<IHostBuilder>, IWebHostFixture)

Creates and returns an IWebHostTest implementation.

public static IWebHostTest Create(Action<IServiceCollection> serviceSetup = null, Action<IApplicationBuilder> pipelineSetup = null, Action<IHostBuilder> hostSetup = null, IWebHostFixture hostFixture = null)

Parameters

serviceSetup Action<IServiceCollection>

The IServiceCollection which may be configured.

pipelineSetup Action<IApplicationBuilder>

The IApplicationBuilder which may be configured.

hostSetup Action<IHostBuilder>

The IHostBuilder which may be configured.

hostFixture IWebHostFixture

An optional IWebHostFixture implementation to use instead of the default ManagedWebHostFixture instance.

Returns

IWebHostTest

An instance of an IWebHostTest implementation.

CreateWithHostBuilderContext(Action<HostBuilderContext, IServiceCollection>, Action<HostBuilderContext, IApplicationBuilder>, Action<IHostBuilder>, IWebHostFixture)

Creates and returns an IWebHostTest implementation.

public static IWebHostTest CreateWithHostBuilderContext(Action<HostBuilderContext, IServiceCollection> serviceSetup = null, Action<HostBuilderContext, IApplicationBuilder> pipelineSetup = null, Action<IHostBuilder> hostSetup = null, IWebHostFixture hostFixture = null)

Parameters

serviceSetup Action<HostBuilderContext, IServiceCollection>

The IServiceCollection which may be configured.

pipelineSetup Action<HostBuilderContext, IApplicationBuilder>

The IApplicationBuilder which may be configured.

hostSetup Action<IHostBuilder>

The IHostBuilder which may be configured.

hostFixture IWebHostFixture

An optional IWebHostFixture implementation to use instead of the default ManagedWebHostFixture instance.

Returns

IWebHostTest

An instance of an IWebHostTest implementation.

RunAsync(Action<IServiceCollection>, Action<IApplicationBuilder>, Action<IHostBuilder>, Func<HttpClient, Task<HttpResponseMessage>>, IWebHostFixture)

Runs a middleware and returns an HttpClient for making HTTP requests to the test server.

public static Task<HttpResponseMessage> RunAsync(Action<IServiceCollection> serviceSetup = null, Action<IApplicationBuilder> pipelineSetup = null, Action<IHostBuilder> hostSetup = null, Func<HttpClient, Task<HttpResponseMessage>> responseFactory = null, IWebHostFixture hostFixture = null)

Parameters

serviceSetup Action<IServiceCollection>

The IServiceCollection which may be configured.

pipelineSetup Action<IApplicationBuilder>

The IApplicationBuilder which may be configured.

hostSetup Action<IHostBuilder>

The IHostBuilder which may be configured.

responseFactory Func<HttpClient, Task<HttpResponseMessage>>

The function delegate that creates a HttpResponseMessage from the HttpClient. Default is a GET request to the root URL ("/").

hostFixture IWebHostFixture

An optional IWebHostFixture implementation to use instead of the default ManagedWebHostFixture instance.

Returns

Task<HttpResponseMessage>

A Task that represents the asynchronous operation. The task result contains the HttpResponseMessage for the test server.

RunWithHostBuilderContextAsync(Action<HostBuilderContext, IServiceCollection>, Action<HostBuilderContext, IApplicationBuilder>, Action<IHostBuilder>, Func<HttpClient, Task<HttpResponseMessage>>, IWebHostFixture)

Runs a filter/middleware test.

public static Task<HttpResponseMessage> RunWithHostBuilderContextAsync(Action<HostBuilderContext, IServiceCollection> serviceSetup = null, Action<HostBuilderContext, IApplicationBuilder> pipelineSetup = null, Action<IHostBuilder> hostSetup = null, Func<HttpClient, Task<HttpResponseMessage>> responseFactory = null, IWebHostFixture hostFixture = null)

Parameters

serviceSetup Action<HostBuilderContext, IServiceCollection>

The IServiceCollection which may be configured.

pipelineSetup Action<HostBuilderContext, IApplicationBuilder>

The IApplicationBuilder which may be configured.

hostSetup Action<IHostBuilder>

The IHostBuilder which may be configured.

responseFactory Func<HttpClient, Task<HttpResponseMessage>>

The function delegate that creates a HttpResponseMessage from the HttpClient. Default is a GET request to the root URL ("/").

hostFixture IWebHostFixture

An optional IWebHostFixture implementation to use instead of the default ManagedWebHostFixture instance.

Returns

Task<HttpResponseMessage>

A Task that represents the asynchronous operation. The task result contains the HttpResponseMessage for the test server.

See Also