When setting up an integration test with the .NET 10.0 SDK and the new xunit v3, I get the following error when trying to customize the WebApplicationFactory:
System.InvalidOperationException: The server has not been started or no web application was configured.
I am using Microsoft.AspNetCore.Mvc.Testing v10.0.0 with the assembly fixture feature of xunit v3
[assembly: AssemblyFixture(typeof(ApiFactory))]
namespace WebApi.Tests.Integration.Fixtures;
public class ApiFactory : WebApplicationFactory<WebApi.Startup>, IAsyncLifetime
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var descriptor =
services.SingleOrDefault(x => x.ServiceType == typeof(DbContextOptions<DbContext>));
if (descriptor != null)
{
services.Remove(descriptor);
}
services.AddDbContext<DbContext>(options
=> options.UseSqlServer("Server=.;Database=Database;Trusted_Connection=True;"));
services.AddHttpClient("Api", client =>
{
client.BaseAddress = new Uri("http://localhost/api/v1/");
});
});
}
public async ValueTask InitializeAsync()
{
}
public new async Task DisposeAsync()
{
await base.DisposeAsync();
}
}
I have a simple integration test that will call the health check endpoint and check for a 200 response.
public class HealthCheckTests
{
private readonly HttpClient _client;
public HealthCheckTests(ApiFactory factory)
{
_client = factory.CreateClient();
}
[Fact]
public async Task Heath_Check_Success_Test()
{
// arrange
// act
var response = await _client.GetAsync("healthcheck", TestContext.Current.CancellationToken);
// assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
}
The problem comes when overriding the ConfigureWebHost method. When doing so I get the error:
System.InvalidOperationException: The server has not been started or no web application was configured.
If I comment out the override the test will succeed but I wont be able to customize the WebApplicationFactory for my testing needs.
Edit
Please see the stack trace below.
System.InvalidOperationException: The server has not been started or no web application was configured. at Microsoft.AspNetCore.TestHost.TestServer.get_Application() at Microsoft.AspNetCore.TestHost.TestServer.CreateHandler() at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory
1.CreateHandler() at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateDefaultClient(DelegatingHandler[] handlers) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateClient(WebApplicationFactoryClientOptions options) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateClient() at CampaignService.WebApi.Tests.Integration.Tests.HealthCheckTests..ctor(ApiFactory factory) in C:\WebApi.Tests.Integration\Tests\HealthCheckTests.cs:line 14 at InvokeStub_HealthCheckTests..ctor(Object, Span1) at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)