Table of Contents

Class ManagedWebHostFixture

Namespace
Codebelt.Extensions.Xunit.Hosting.AspNetCore
Assembly
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

ManagedWebHostFixture()

Initializes a new instance of the ManagedWebHostFixture class.

public ManagedWebHostFixture()

Properties

Application

Gets the IApplicationBuilder initialized by the IHost.

public IApplicationBuilder Application { get; protected set; }

Property Value

IApplicationBuilder

The IApplicationBuilder initialized by the IHost.

ConfigureApplicationCallback

Gets or sets the delegate that configures the HTTP request pipeline.

public Action<IApplicationBuilder> ConfigureApplicationCallback { get; set; }

Property Value

Action<IApplicationBuilder>

The delegate that configures the HTTP request pipeline.

Methods

ConfigureHost(Test)

Creates and configures the IWebHost of this instance.

public override void ConfigureHost(Test hostTest)

Parameters

hostTest Test

The object that inherits from WebHostTest<T>.

Remarks

hostTest was added to support those cases where the caller is required in the host configuration.

Exceptions

ArgumentNullException

hostTest is null.

ArgumentOutOfRangeException

hostTest is not assignable from WebHostTest<T>.

See Also