I'm trying to add some integration tests to my existing .net 6.0 project, but keep running into issues
Right now if I run the test, it hits the line await host.RunAsync(CancellationToken.None);
in Program.cs
and then just hangs forever and seemingly does nothing
Have I misunderstoof something? If not how can I troubleshoot this?
Program.cs
public /*static*/ class Program
{
public static async Task Main(string[] args)
{
var host = BuildWebHost(args);
await host.InitialiseAsync(CancellationToken.None);
await host.SeedAsync(CancellationToken.None);
await host.RunAsync(CancellationToken.None); // hangs here
}
}
Tests.cs
[Fact]
public void Test1Async()
{
using (var application = new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
builder.ConfigureServices(services =>
{
// set up servises
});
}))
{
var c = application.CreateClient(); // hangs here
}
}
It does eventually time out and give me this error
Message: System.InvalidOperationException : Timed out waiting for the entry point to build the IHost after 00:05:00. This timeout can be modified using the 'DOTNET_HOST_FACTORY_RESOLVER_DEFAULT_TIMEOUT_IN_SECONDS' environment variable.
Stack Trace: HostingListener.CreateHost() <>c__DisplayClass10_0.b__0(String[] args) DeferredHostBuilder.Build() WebApplicationFactory
1.CreateHost(IHostBuilder builder) DelegatedWebApplicationFactory.CreateHost(IHostBuilder builder) WebApplicationFactory
1.ConfigureHostBuilder(IHostBuilder hostBuilder) WebApplicationFactory1.EnsureServer() WebApplicationFactory
1.CreateDefaultClient(DelegatingHandler[] handlers) WebApplicationFactory1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers) WebApplicationFactory
1.CreateClient(WebApplicationFactoryClientOptions options) WebApplicationFactory`1.CreateClient() UnitTest1.Test1Async() line 35
Even though you didn't share the code for BuildWebHost()
method I assume it is something like this public static IWebHostBuilder BuildWebHost(string[] args)
.
If so then simply rename this method to CreateWebHostBuilder
. Name of the method is important because WebApplicationFactory
internally is looking for it.
Straight from the summary on the constructor:
/// The <see cref="WebApplicationFactory{TEntryPoint}"/> will find the entry point class of <typeparamref name="TEntryPoint"/>
/// assembly and initialize the application by calling <c>IWebHostBuilder CreateWebHostBuilder(string [] args)</c>
/// on <typeparamref name="TEntryPoint"/>.
Source: https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs#L24