ManagedWebHostFixture Class
Definition
- Namespace
- Codebelt.Extensions.Xunit.Hosting.AspNetCore
- Assemblies
- Codebelt.Extensions.Xunit.Hosting.AspNetCore.dll
Provides a default implementation of the IWebHostFixture interface.
public class ManagedWebHostFixture : ManagedHostFixture, IAsyncLifetime, IWebHostFixture, IGenericHostFixture, IHostFixture, IConfigurationTest, IEnvironmentTest, IDisposable, IAsyncDisposable, IPipelineTest
- Inheritance
-
ManagedWebHostFixture
- Implements
-
IAsyncLifetime
- Derived
- Inherited Members
- Extension Methods
Examples
The following example uses ManagedWebHostFixture as the fixture type for a web host test class. The fixture owns the full IWebHost lifecycle, starting the web host before tests and disposing it after, making it suitable for integration testing of middleware, controllers, and Razor Pages.
using System.Threading.Tasks;
using Codebelt.Extensions.Xunit.Hosting.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace WebFixtureTests;
public class MiddlewareTest : WebHostTest<ManagedWebHostFixture>
{
public MiddlewareTest(ManagedWebHostFixture hostFixture, ITestOutputHelper output) : base(hostFixture, output)
{
}
public override void ConfigureServices(IServiceCollection services)
{
services.AddFakeHttpContextAccessor();
}
public override void ConfigureApplication(IApplicationBuilder app)
{
app.UseMiddleware<TestMiddleware>();
}
[Fact]
public async Task Middleware_ShouldAddHeader()
{
var pipeline = Application.Build();
var context = new DefaultHttpContext();
await pipeline(context);
Assert.True(context.Response.Headers.ContainsKey("X-Test"));
}
}
public class TestMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
context.Response.Headers["X-Test"] = "true";
await next(context);
}
}
Constructors
| Name | Description |
|---|---|
| ManagedWebHostFixture() | Initializes a new instance of the ManagedWebHostFixture 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 IWebHost of this instance. |