Created
August 7, 2013 05:07
-
-
Save weavenet/6171363 to your computer and use it in GitHub Desktop.
Ruby method to stub out ENV variables in Rspec.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module EnvVar | |
| def set_env_var(name, value) | |
| ENV.stub(:[]) | |
| ENV.stub(:[]).with(name).and_return(value) | |
| end | |
| end |
@aldrienht Try this:
allow(ENV).to receive(:[]).with(name, second_param).and_return(value)
The climate_control gem is great for this use case, if you only need to support Ruby >= 2.5.
The stub_env or rspec-stubbed_env gems provide this functionality for both older and newer rubies.
I'm doing this: https://gist.github.com/henrik/8f4a9fc07e42a3a410a1049af2631bd9
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you try these ways:
stub_const('ENV', ENV.to_hash.merge('HOST' => 'http://sample.host'))stub_const('ENV', 'HOST' => 'www.aaa.com')