"No app configured" 使用 WebApplicationFactory 进行 运行 集成测试时出错

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

我正在为使用 .net5 的应用程序编写集成测试我已经使用 WebApplicationFactory 和 IHostBuilder 来设置环境。

自定义夹具 ->

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) 调用注册工作流的 UnityContainer(https://github.com/danielgerlag/workflow-core).

测试class(测试为运行时出错)-> Error

错误描述 -> 未配置应用程序。请通过IWebHostBuilder.UseStartup、IWebHostBuilder.Configure指定应用程序,或通过Web主机配置中的StartupAssemblyKey指定启动程序集

添加此代码:.ConfigureWebHostDefaults(b => b.Configure(app => {})); .ConfigureServices 通话后

您使用的辅助服务不是 运行 网络主机。但是,WebApplicationFactory 仍然需要一个,因此创建一个空的 Web 应用程序。