1

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.WebApplicationFactory1.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)

3
  • Can you add the full information from the exception including stack trace and any inner exceptions? Commented 7 hours ago
  • @user700390 please see the stack trace above Commented 6 hours ago
  • 1
    Can you please post a full minimal reproducible example somewhere (for example @github) for lazy person like me? Commented 5 hours ago

1 Answer 1

2

It looks like you are not calling base.ConfigureWebHost() in your method override on the subclass. I think this is the most likely culprit here.

Sign up to request clarification or add additional context in comments.

1 Comment

I am sorry I should have mentioned that I have called that both before and after the custom setup with the same error message.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.