Class MinimalWebHostTestFactory
- 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 (minimal style).
public static class MinimalWebHostTestFactory
- Inheritance
-
MinimalWebHostTestFactory
Examples
Use MinimalWebHostTestFactory when a test needs the modern IHostApplicationBuilder configuration model and a small in-memory request pipeline. The example supplies a health state through dependency injection, serves it from middleware, and reads the response returned by RunAsync.
using System.Threading.Tasks;
using Codebelt.Extensions.Xunit.Hosting.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace HealthEndpoint.Tests;
public sealed class MinimalWebHostTestFactoryExample
{
public async Task<string> ReadHealthStateAsync()
{
using var response = await MinimalWebHostTestFactory.RunAsync(
services => services.AddSingleton(new HealthState("healthy")),
app => app.Run(context =>
{
var health = context.RequestServices.GetRequiredService<HealthState>();
return context.Response.WriteAsync(health.Value);
})).ConfigureAwait(false);
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
public sealed record HealthState(string Value);
Methods
Create(Action<IServiceCollection>, Action<IApplicationBuilder>, Action<IHostApplicationBuilder>, IWebMinimalHostFixture)
Creates and returns an IWebHostTest implementation.
public static IWebHostTest Create(Action<IServiceCollection> serviceSetup = null, Action<IApplicationBuilder> pipelineSetup = null, Action<IHostApplicationBuilder> hostSetup = null, IWebMinimalHostFixture hostFixture = null)
Parameters
serviceSetupAction<IServiceCollection>The IServiceCollection which may be configured.
pipelineSetupAction<IApplicationBuilder>The IApplicationBuilder which may be configured.
hostSetupAction<IHostApplicationBuilder>The IHostBuilder which may be configured.
hostFixtureIWebMinimalHostFixtureAn 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<IHostApplicationBuilder>, IWebMinimalHostFixture)
Creates and returns an IWebHostTest implementation.
public static IWebHostTest CreateWithHostBuilderContext(Action<HostBuilderContext, IServiceCollection> serviceSetup = null, Action<HostBuilderContext, IApplicationBuilder> pipelineSetup = null, Action<IHostApplicationBuilder> hostSetup = null, IWebMinimalHostFixture hostFixture = null)
Parameters
serviceSetupAction<HostBuilderContext, IServiceCollection>The IServiceCollection which may be configured.
pipelineSetupAction<HostBuilderContext, IApplicationBuilder>The IApplicationBuilder which may be configured.
hostSetupAction<IHostApplicationBuilder>The IHostBuilder which may be configured.
hostFixtureIWebMinimalHostFixtureAn 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<IHostApplicationBuilder>, Func<HttpClient, Task<HttpResponseMessage>>, IWebMinimalHostFixture)
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<IHostApplicationBuilder> hostSetup = null, Func<HttpClient, Task<HttpResponseMessage>> responseFactory = null, IWebMinimalHostFixture hostFixture = null)
Parameters
serviceSetupAction<IServiceCollection>The IServiceCollection which may be configured.
pipelineSetupAction<IApplicationBuilder>The IApplicationBuilder which may be configured.
hostSetupAction<IHostApplicationBuilder>The IHostBuilder which may be configured.
responseFactoryFunc<HttpClient, Task<HttpResponseMessage>>The function delegate that creates a HttpResponseMessage from the HttpClient. Default is a GET request to the root URL ("/").
hostFixtureIWebMinimalHostFixtureAn 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<IHostApplicationBuilder>, Func<HttpClient, Task<HttpResponseMessage>>, IWebMinimalHostFixture)
Runs a filter/middleware test.
public static Task<HttpResponseMessage> RunWithHostBuilderContextAsync(Action<HostBuilderContext, IServiceCollection> serviceSetup = null, Action<HostBuilderContext, IApplicationBuilder> pipelineSetup = null, Action<IHostApplicationBuilder> hostSetup = null, Func<HttpClient, Task<HttpResponseMessage>> responseFactory = null, IWebMinimalHostFixture hostFixture = null)
Parameters
serviceSetupAction<HostBuilderContext, IServiceCollection>The IServiceCollection which may be configured.
pipelineSetupAction<HostBuilderContext, IApplicationBuilder>The IApplicationBuilder which may be configured.
hostSetupAction<IHostApplicationBuilder>The IHostBuilder which may be configured.
responseFactoryFunc<HttpClient, Task<HttpResponseMessage>>The function delegate that creates a HttpResponseMessage from the HttpClient. Default is a GET request to the root URL ("/").
hostFixtureIWebMinimalHostFixtureAn 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.