dotnet / dotnet/aspnetcore

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

Open
#53,370 1 comment 0 reactions 0 assignees View on GitHub
area-minimal area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] 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](https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-8.0#basic-tests-with-the-default-webapplicationfactory), is used as the type provided to `WebApplicationFactory`.

In my Program.cs I have:

```c#
// the usual ASP.NET configuration code

public partial class Program { }
// end of file
```

In my test suite I have:

```c#
public class TestExamplesProgram : Program { }

public class TestExamples : IClassFixture>
{

private WebApplicationFactory Factory { get; }

public TestExamples(WebApplicationFactory 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](https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-8.0#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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.