Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an ASP.NET Core environment variable in testing with Visual Studio

In my integration tests, I want to set a specific connection string when the test runs in a development environment, and another connection string when the test runs in the staging environment.

When I am not in testing mode, I simply set the environment variable on the machine, and all work OK. But on testing I can use UseEnvironment(envX), but then it'll be envX on all machines, or not use this method, and get the default one (which is production).

So, how can I use multiple connection strings, environment based, in my integration tests?

like image 717
arielorvits Avatar asked Mar 11 '23 09:03

arielorvits


1 Answers

If you use IHostingEnvironment to check the environment in integration test code, then you may override value in IHostingEnvironment.EnvironmentName:

//IHostingEnvironment env;

env.EnvironmentName = 'Development';
env.IsDevelopment() // return true;

env.EnvironmentName = 'TEST';
env.IsDevelopment() // return false;
env.IsEnvironment('TEST') // return true;
like image 184
Set Avatar answered Apr 12 '23 15:04

Set