簡體   English   中英

使用 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 )。

測試類(運行測試時出錯)->錯誤

錯誤描述 -> 未配置應用程序。 請通過 IWebHostBuilder.UseStartup、IWebHostBuilder.Configure 指定應用程序,或通過 Web 主機配置中的 StartupAssemblyKey 指定啟動程序集

添加此代碼: .ConfigureWebHostDefaults(b => b.Configure(app => {})); .ConfigureServices調用之后

您正在使用不運行網絡主機的 Worker Service。 但是,WebApplicationFactory 仍然需要一個,因此創建一個空的 Web 應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM