How to Write Specs for Code That Depends on Environment Variables

What is the best way to write specs for code that depends on environment variables?

That would work.

Another way would be to put a layer of indirection between your code and the environment variables, like some sort of configuration object that's easy to mock.

Testing config module with mocha that depends on environment variable process.env.APP_ENV

import statements are executed before any other code, so you can't make this work using import.

You can somewhat get it working with require, if you require the parameters file after you have set the environment variable:

process.env.APP_ENV = 'production';
let params = require('../../../parameters').default;
...

However, this still isn't great, because it'll work just once because of the cache that require maintains (a subsequent test which sets APP_ENV to a different value won't work).

A workaround would be to have parameters.js export a function that you'd call:

// parameters.js
export default function() {
let params = devParams;
switch (process.env.APP_ENV) {
...
}
return params;
}

// test.js
import getParams from '../../../parameters';
...

process.env.APP_ENV = 'production';
let params = getParams();

Or set APP_ENV externally:

$ env APP_ENV='production' mocha ...

What order of reading configuration values?

The standard that I know is first look for a command line parameter, if not found environment var, then local config file then global config file.

So when a package is installed somewhere. It will have a default config file. This can be changed with a local config file. Which can be overrridden with a environ parameter and then the command line param has the highest precedence.

If a config file is declared on the command line then its contents will take precedence over environ params or any other config files. But command line params will take precedence over it.
But remember that the search path still exists. If the package is looking for a var it looks for.

Command line.
Config file thats name is declared on the command line.
Environment vars
Local config file (if exists)
Global config file (if exists)

I think many command line compilers and the Boost library config pak works in a similar fashion

Is there a way to test environment defined dynamic validations in a model with RSpec?

In simple words:

I would imagine that once you're running the tests altogether, the files are required and the validator is defined once.

Thus the value of the presence key remains the same even after you change the value of ENV['NETWORK'] in the test.

Use a validation method that reads the environment variable each time the object is validated.

Here is an example for the sample code you shared:

validate :validator_name

def validator_name
if (ENV['NETWORK'] != 'testnet3') && output_address.blank?
# add errors here
end
end

.env not being loaded in test environment in Rails with rspec

Dotenv.load(File.expand_path("../../.env.#{Rails.env}", __FILE__))

upon researching Paritosh's answer I found that the file needed to specified per environment.

How to set env variable in Jupyter notebook

To set an env variable in a jupyter notebook, just use a % magic commands, either %env or %set_env, e.g., %env MY_VAR=MY_VALUE or %env MY_VAR MY_VALUE. (Use %env by itself to print out current environmental variables.)

See: http://ipython.readthedocs.io/en/stable/interactive/magics.html



Related Topics



Leave a reply



Submit