views:

45

answers:

4

The code has a runtime dependency which is not available in our development environment (and is available in test and prod). It's expensive to actually test for the dependency, and I want to test for the environment instead.

if (isDevEnvironment) {
    // fake it
}
else {
    // actually do it
}

Without using appSettings, what code/technique/test would you use to set isDevEnvironment?

Example answers:

  • check machine name (partial or full)
  • check for running instance of Visual Studio
  • check for environment variable

I'm hoping for a test I hadn't considered.

A: 

You've hit upon the major techniques. At my current job, we use the Enviroment variable technique.

At a previous job, all servers had three NIC's, there was the public front end, the middle tier for server to server traffic, and the back end Network Operations would connect to.

There were on different IP subnets. It made it easy to detect where something was coming from, but also who where was it.

Example:

  • 10.100.x.xxx - Production Subnet
  • 10.100.1.xxx - Back
  • 10.100.2.xxx - Middle
  • 10.100.3.xxx - Front

  • 10.0.1.x - Development Subnet

This required nothing to be installed special on the servers, just code detection and then caching.

Digicoder
+2  A: 

The code you provided (if (isDevEnvironment) ..) smells with test code in production.

Without using appSettings, what code/technique/test would you use to set isDevEnvironment?

Generally, Dependency Injection.

But also the the possible Solution in the link provided.

You should not check the environment, instead you need to provide the environment.

Dmytrii Nagirniak
+3  A: 

You should try to not test your environment in the code! That's why dependency inversion (and then injection) has been invented for.

Draw some inspiration from NewSpeak, where where the complete platform is abstracted in an object and passed as parameter down the chain of method calls.

ewernli
A: 

I prefer to do this:

if(Properties.Settings.Default.TestEnvironment || HttpContext.Current.Request.ServerVariables["Server_Name"] == "localhost")
{
 // do something
}
citronas