使用ASP.NET Core进行集成测试期间使用多个环境 [英] Working with multiple environments during integration testing with ASP.NET Core
问题描述
这是测试项目的launchSettings.json文件:
Here's the launchSettings.json file for the test project:
{
"profiles": {
"test": {
"commandName": "test",
"environmentVariables": {
"Hosting:Environment": "Development"
}
}
}
}
测试项目依赖于服务器项目,并使用服务器项目的Startup
类直接初始化测试,如下所示:
The test project has a dependency on the server project, and initializes the tests using the server project's Startup
class directly like so:
Server = new TestServer(TestServer.CreateBuilder().UseStartup<Startup>());
Client = Server.CreateClient();
但是由于某种原因,当我在调试器中输入Startup.Configure
方法时,env.EnvironmentName
是Production.我应该在哪里设置测试.xproj的环境名称?
Yet for some reason, when I enter the Startup.Configure
method in the debugger, env.EnvironmentName
is Production. Where am I supposed to set the environment name for the test .xproj?
在相关说明中,我应该在测试项目中拥有Startup
类的本地测试版本吗? 文档似乎暗示了其他建议. ..这些设置来自何处?他们没有将测试项目显示为在本地具有appsettings的副本,但是我很确定Startup
类(无论是重用的还是本地的)都将需要它.请告知.
On a related note, should I have a local test version of the Startup
class in the test project? The docs seem to suggest otherwise... And where will the appsettings come from? They don't show the test project as having a copy of the appsettings locally, but I'm pretty sure the Startup
class (whether reused or local) will need it. Please advise.
推荐答案
您可以使用UseEnvironment方法.
You can use the UseEnvironment method.
Server = new TestServer(TestServer.CreateBuilder().UseEnvironment("Testing").UseStartup<Startup>());
Server = new TestServer(TestServer.CreateBuilder().UseEnvironment("Testing").UseStartup<Startup>());
这篇关于使用ASP.NET Core进行集成测试期间使用多个环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!