Search code examples
c#asp.net-coreintegration-testingasp.net-core-5.0

"No app configured" error while using WebApplicationFactory for running integration tests


I am writing integration tests for my app which uses .net5 I have used WebApplicationFactory with IHostBuilder for setting up environment.

Custom fixture ->

public class TestFixture<T> : WebApplicationFactory<Program>
{
    protected override IHostBuilder CreateHostBuilder()
    {
       var builder = Host.CreateDefaultBuilder();
       builder.ConfigureAppConfiguration((hostingContext, config) =>
       {
           var env = hostingContext.HostingEnvironment;
           config.SetBasePath(env.ContentRootPath);
           config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
           config.AddEnvironmentVariables();
       })
       .ConfigureServices((hostContext, services) => { services.Configure(hostContext); });
       return builder;
   }
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
    builder.ConfigureTestServices((services) =>
    {
        services.RemoveAll(typeof(IHostedService));
    });
}

}

services.Configure(hostContext) calls UnityContainer which registers workflows(https://github.com/danielgerlag/workflow-core).

Test class(gives error when test is run)-> Error

Error desc -> No application configured. Please specify an application via IWebHostBuilder.UseStartup, IWebHostBuilder.Configure, or specifying the startup assembly via StartupAssemblyKey in the web host configuration


Solution

  • Add this code : .ConfigureWebHostDefaults(b => b.Configure(app => {})); after .ConfigureServices call

    you are using a Worker Service that doesn't run a web host. However, the WebApplicationFactory still expects one, So create an empty web application.