问题
Here's the launchSettings.json file for the test project:
{
"profiles": {
"test": {
"commandName": "test",
"environmentVariables": {
"Hosting:Environment": "Development"
}
}
}
}
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();
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?
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.
回答1:
You can use the UseEnvironment method.
Server = new TestServer(TestServer.CreateBuilder().UseEnvironment("Testing").UseStartup<Startup>());
回答2:
Using .UseEnvironment("Development") will get you detailed error messages and etc
来源:https://stackoverflow.com/questions/35734681/working-with-multiple-environments-during-integration-testing-with-asp-net-core