Class BlockingManagedWebApplicationFixture<TEntryPoint>
- Namespace
- Codebelt.Extensions.Xunit.Hosting.AspNetCore
- Assembly
- Codebelt.Extensions.Xunit.Hosting.AspNetCore.dll
Provides a blocking managed implementation of the IWebApplicationFixture<TEntryPoint> interface.
public class BlockingManagedWebApplicationFixture<TEntryPoint> : HostFixture, IAsyncLifetime, IWebApplicationFixture<TEntryPoint>, IHostFixture, IConfigurationTest, IEnvironmentTest, IDisposable, IAsyncDisposable where TEntryPoint : class
Type Parameters
TEntryPointA type in the entry point assembly of the application.
- Inheritance
-
BlockingManagedWebApplicationFixture<TEntryPoint>
- Implements
-
IAsyncLifetimeIWebApplicationFixture<TEntryPoint>
- Inherited Members
- Extension Methods
Examples
The test project references a minimal ASP.NET Core application and shares its in-memory server through xUnit's class-fixture lifetime. BlockingManagedWebApplicationFixture<TEntryPoint> waits for startup before constructing the test class, so the test can create a client from TestServer and exercise the real request pipeline immediately.
using System.Threading.Tasks;
using Codebelt.Extensions.Xunit.Hosting.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace CatalogApi.Tests;
public sealed class CatalogApiTest : IClassFixture<BlockingManagedWebApplicationFixture<CatalogProgram>>
{
private readonly BlockingManagedWebApplicationFixture<CatalogProgram> _fixture;
public CatalogApiTest(BlockingManagedWebApplicationFixture<CatalogProgram> fixture)
{
_fixture = fixture;
}
[Fact]
public async Task HealthEndpoint_ReturnsApplicationState()
{
using var client = _fixture.Server.CreateClient();
var body = await client.GetStringAsync("/health").ConfigureAwait(false);
Assert.Equal("ready", body);
}
}
public sealed record CatalogStatus(string Value);
public sealed class CatalogProgram
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(new CatalogStatus("ready"));
var app = builder.Build();
app.MapGet("/health", (CatalogStatus status) => status.Value);
app.Run();
}
}
Remarks
Unlike the base managed web host fixtures, this fixture starts the resolved application host synchronously. ASP.NET Core application entry point testing must expose a started TestServer after fixture initialization. There is no separate non-blocking managed web application fixture because this application-entry-point API is new and unreleased.
Constructors
BlockingManagedWebApplicationFixture()
Initializes a new instance of the BlockingManagedWebApplicationFixture<TEntryPoint> class.
public BlockingManagedWebApplicationFixture()
Properties
ConfigureWebHostCallback
Gets or sets the delegate that provides a way to override the IWebHostBuilder before the application is built.
public Action<IWebHostBuilder> ConfigureWebHostCallback { get; set; }
Property Value
- Action<IWebHostBuilder>
The delegate that provides a way to override the IWebHostBuilder.
Server
Gets the TestServer initialized by this instance.
public TestServer Server { get; protected set; }
Property Value
- TestServer
The TestServer initialized by this instance.
Methods
ConfigureHost(Test)
Creates and configures the IHost of this instance.
public virtual void ConfigureHost(Test hostTest)
Parameters
hostTestTestThe object that inherits from WebApplicationTest<TEntryPoint, T>.
Remarks
hostTest was added to support those cases where the caller is required in the host configuration.
Exceptions
- ArgumentNullException
hostTestis null.- ArgumentOutOfRangeException
hostTestis not assignable from WebApplicationTest<TEntryPoint, T>.