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
What Alternatives to Irb Are There
Better Way to Turn a Ruby Class into a Module Than Using Refinements
Neither Ruby and Nor Irb Can Load .Rb File in Current Directory
Calling/Applying Lambda VS. Function Call - the Syntax in Ruby Is Different. Why
How to Get Searchlogic to Work with Rails 3
Rails 3 Initializes Extremely Slow on Ruby 1.9.2
How to Use Ruby Minitest::Spec with Rails for API Integration Tests
Ruby Unable to Parse a CSV File: CSV::Malformedcsverror (Illegal Quoting in Line 1.)
Ruby Singleton Methods for Class and Objects
Getting a Dns Txt Record in Ruby
Array.Include? Multiple Values
How to Reinstall a Gem Using Bundler
Profile a Rails Controller Action
Sorting a Two-Dimensional Array by Second Value
Ruby: Get All Keys in a Hash (Including Sub Keys)
Anybody Tried the Crystal Programming Language (Machine-Code Compiled Ruby)