Skip to content

Exception raised from Factory.CreateClient() when type provided to WebApplicationFactory<TEntryPoint> is a subclass of Program #53370

@br3nt

Description

@br3nt

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

An exception is raised from Factory.CreateClient() when a subclass of Program, added to Program.cs as described in Basic tests with the default WebApplicationFactory, is used as the type provided to WebApplicationFactory<TEntryPoint>.

In my Program.cs I have:

// the usual ASP.NET configuration code

public partial class Program { }
// end of file

In my test suite I have:

public class TestExamplesProgram : Program { }

public class TestExamples : IClassFixture<WebApplicationFactory<TestExamplesProgram>>
{

    private WebApplicationFactory<TestExamplesProgram> Factory { get; }

    public TestExamples(WebApplicationFactory<TestExamplesProgram> factory)
    {
        Factory = factory;
    }

    [Fact]
    public async Task ExampleTestWithException()
    {
        try
        {
            var client = Factory.CreateClient(); // exception raised here
        }
        catch (Exception ex)
        {
            throw;
        }
}

Expected Behavior

The reason I create a subclass of Program, is so that I can add a ConfigureDatabase() method which then gets overridden in the TestExamplesProgram subclass.

I thought I could do this instead of removing and reconfiguring as described in Customize WebApplicationFactory.

I thought the approach of removing configured services could lead to unexpected behaviors in my test suite, so better to override the configuration.

Steps To Reproduce

Run the only test found in the ASP.NET MVC project found at https://github.com/br3nt/WebApplicationFactoryException

Exceptions (if any)

The exception is System.InvalidOperationException with message "The entry point exited without ever building an IHost.".

The stack trace is:

   at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.CreateHost()
   at Microsoft.Extensions.Hosting.HostFactoryResolver.<>c__DisplayClass10_0.b__0(String[] args)
   at Microsoft.AspNetCore.Mvc.Testing.DeferredHostBuilder.Build()
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateHost(IHostBuilder builder)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.ConfigureHostBuilder(IHostBuilder hostBuilder)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.EnsureServer()
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient()
   at Tests.TestExamples.d__4.MoveNext() in C:\PathToSolution\Tests\TestExamples.cs:line 51

There is no inner exception.

.NET Version

8.0.100

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions