Working with multiple environments during integration testing with ASP.NET Core

跟風遠走 提交于 2019-12-10 09:28:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!