Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 243
gzak Avatar asked Feb 08 '23 14:02

gzak


1 Answers

You can use the UseEnvironment method.

Server = new TestServer(TestServer.CreateBuilder().UseEnvironment("Testing").UseStartup<Startup>());

like image 76
davidfowl Avatar answered Apr 23 '23 14:04

davidfowl