ManagedWebMinimalHostFixture Class
Definition
- Namespace
- Codebelt.Extensions.Xunit.Hosting.AspNetCore
- Assemblies
- Codebelt.Extensions.Xunit.Hosting.AspNetCore.dll
Provides a default implementation of the IWebMinimalHostFixture interface.
public class ManagedWebMinimalHostFixture : ManagedMinimalHostFixture, IAsyncLifetime, IWebMinimalHostFixture, IMinimalHostFixture, IHostFixture, IConfigurationTest, IEnvironmentTest, IDisposable, IAsyncDisposable, IPipelineTest
- Inheritance
-
ManagedWebMinimalHostFixture
- Implements
-
IAsyncLifetime
- Derived
- Inherited Members
- Extension Methods
Examples
The following example uses ManagedWebMinimalHostFixture as the fixture type for a minimal web host test class. The fixture starts and manages a minimal web host, giving the test access to a configured host and the ability to send HTTP requests through the ASP.NET Core pipeline.
using System.Threading.Tasks;
using Codebelt.Extensions.Xunit.Hosting.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
namespace WebFixtureTests;
public class MinimalApiTest : MinimalWebHostTest<ManagedWebMinimalHostFixture>
{
public MinimalApiTest(ManagedWebMinimalHostFixture hostFixture, ITestOutputHelper output) : base(hostFixture, output)
{
}
protected override void ConfigureHost(IHostApplicationBuilder hb)
{
hb.Services.AddFakeHttpContextAccessor();
}
public override void ConfigureApplication(IApplicationBuilder app)
{
app.UseMiddleware<TestMiddleware>();
}
}
public class TestMiddleware(RequestDelegate next)
{
public async Task InvokeAsync(HttpContext context)
{
context.Response.Headers["X-Test"] = "true";
await next(context);
}
}
public sealed class RunExample(ManagedWebMinimalHostFixture fixture, ITestOutputHelper output) : MinimalApiTest(fixture, output)
{
[Fact]
public async Task BuildsAndInvokesTheConfiguredPipeline()
{
var context = new DefaultHttpContext
{
RequestServices = Application.ApplicationServices
};
await Application.Build()(context);
Assert.Equal("true", context.Response.Headers["X-Test"].ToString());
Assert.NotNull(context.RequestServices.GetRequiredService<IHttpContextAccessor>());
}
}
Remarks
This is the "modern" minimal style implementation of ManagedWebHostFixture.
Constructors
| Name | Description |
|---|---|
| ManagedWebMinimalHostFixture() | Initializes a new instance of the ManagedWebMinimalHostFixture class. |
Properties
| Name | Description |
|---|---|
| Application | Gets the IApplicationBuilder initialized by the IHost. |
| ConfigureApplicationCallback | Gets or sets the delegate that configures the HTTP request pipeline. |
Methods
| Name | Description |
|---|---|
| ConfigureHost(Test) | Creates and configures the IHost of this instance. |