9

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

2
  • Why are you using a package exclusively created for testing ASP.NET Core applications when you are not using ASP.NET Core? This approach makes no sense. Commented Nov 15, 2021 at 12:29
  • 2
    @CamiloTerevinto What should I use to add integration tests in this scenario? My app is a console app with target framework .net 5 and there are two HostBuilder running which are used to add two hosted services. One is used to read messages from a queue and add to db and other reads db and starts workflow as mentioned in original question. Commented Nov 15, 2021 at 18:17

3 Answers 3

12

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.

Sign up to request clarification or add additional context in comments.

Comments

1

@Vaibhav's answer above was very helpful but still not super obvious on how to apply if working directly with WebApplicationFactory in .Net6+ using minimal hosting. Hopefully this is helpful to someone else...

using WebApplicationFactory<Program> webApplicationFactory = 
    new WebApplicationFactory<Program>()
        .WithWebHostBuilder(builder =>
        {
            builder.Configure(_ => { });
        });

Without that empty configure, you will get the following error and it was driving me mad!

System.InvalidOperationException: 'No application configured. 
Please specify an application via IWebHostBuilder.UseStartup, 
IWebHostBuilder.Configure, or specifying the startup assembly 
via StartupAssemblyKey in the web host configuration.'

1 Comment

This post has been viewed over 3400 times as of today, so I'm going to assume devs are being directed here in their quest to create an end-to-end test of a .net worker service. If this is you, please have a look at my answer on how you can use WebApplicationFactory to accomplish this. stackoverflow.com/a/77734096/4465532
0

For me I forgot to add app.Run() at the end.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.