我正在为使用 .net5 的应用编写集成测试。我使用了 WebApplicationFactory 和 IHostBuilder 来设置环境。
自定义 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)调用UnityContainer注册工作流(https://github.com/danielgerlag/workflow-core)。
测试类(运行测试时出错)-> 错误
错误描述 -> 未配置应用程序。请通过IWebHostBuilder.UseStartup、IWebHostBuilder.Configure或在Web主机配置中指定启动程序集的StartupAssemblyKey来指定应用程序