Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"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));
    });
}

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading