ManagedWebApplicationFixture<TEntryPoint> Class
Definition
- Namespace
- Codebelt.Extensions.Xunit.Hosting.AspNetCore
- Assemblies
- Codebelt.Extensions.Xunit.Hosting.AspNetCore.dll
Provides an entrypoint-owned implementation of the IWebApplicationFixture<TEntryPoint> interface.
public class ManagedWebApplicationFixture<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
-
ManagedWebApplicationFixture<TEntryPoint>
- Implements
-
IAsyncLifetimeIWebApplicationFixture<TEntryPoint>
- Inherited Members
- Extension Methods
Examples
Use ManagedWebApplicationFixture<TEntryPoint> when an xUnit class fixture should exercise an ASP.NET Core application's real entry point and let that entry point start the in-memory server. Derive the test from WebApplicationTest<TEntryPoint,T> and pass the fixture to its base constructor so the base class initializes the fixture through ConfigureHost before the test reads Server. Fixture setup remains lazy; consuming the test host starts the deferred host, after which the test can create a client from the exposed TestServer and verify the application's endpoint behavior. WebApplicationTestFactory uses this fixture by default.
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 : WebApplicationTest<CatalogProgram, ManagedWebApplicationFixture<CatalogProgram>>
{
public CatalogApiTest(ManagedWebApplicationFixture<CatalogProgram> fixture, ITestOutputHelper output)
: base(fixture, output)
{
}
[Fact]
public async Task HealthEndpoint_ReturnsApplicationState()
{
using var client = 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
The ASP.NET Core application's Main method owns host startup. The fixture captures the host after it has been built without starting it during fixture setup; the test host starts the deferred host when it is consumed.
Constructors
| Name | Description |
|---|---|
| ManagedWebApplicationFixture() | Initializes a new instance of the ManagedWebApplicationFixture<TEntryPoint> class. |
Properties
| Name | Description |
|---|---|
| ConfigureWebHostCallback | Gets or sets the delegate that provides a way to override the IWebHostBuilder before the application is built. |
| Server | Gets the TestServer initialized by this instance. |
Methods
| Name | Description |
|---|---|
| ConfigureHost(Test) | Creates and configures the IHost of this instance. |