gpt4 book ai didi

c# - 在集成测试中设置环境

转载 作者:行者123 更新时间:2023-12-03 20:50:51 25 4
gpt4 key购买 nike

我想使用 WebApplicationFactory 在我的集成测试中设置环境.默认情况下,env 设置为 Development .我的 Web 应用程序工厂的代码如下所示:

public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup>
where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseSolutionRelativeContentRoot(AppContext.BaseDirectory);

base.ConfigureWebHost(builder);
}

protected override IWebHostBuilder CreateWebHostBuilder()
{
return WebHost.CreateDefaultBuilder()
.UseStartup<TStartup>()
.UseEnvironment("test"); // i want to launch `test` environment while im
// testing my app
}
}
当我开始调试时 Startup类(当我运行测试时)我仍然收到 Development环境:
public Startup(IHostEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
// env.EnvironmentName is set to 'Development' while i set it to 'test'
// in my WebApplicationFactory
.AddEnvironmentVariables();

Configuration = builder.Build();
}
如何在 WebApplicationFactory 中设置环境适本地?或者也许如何仅在启动时更改测试策略我取决于 appsettings文件?

最佳答案

我遇到了同样的问题,以下对我有用:

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Test");
}

关于c# - 在集成测试中设置环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62912223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com