小编Anu*_*hur的帖子

使用 WebApplicationFactory 运行集成测试时出现“未配置应用程序”错误

我正在为使用 .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));
    });
}
Run Code Online (Sandbox Code Playgroud)

}

services.Configure(hostContext) 调用注册工作流的 UnityContainer ( https://github.com/danielgerlag/workflow-core )。

测试类(运行测试时出错)->错误

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

c# integration-testing asp.net-core asp.net-core-5.0

7
推荐指数
1
解决办法
3902
查看次数