-
-
Save BrianSigafoos/47df273009c49842b8b7a09e2742bcff to your computer and use it in GitHub Desktop.
Simple method to mock environment variable in ruby with minitest or other testing framework
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
# in test_helper.rb (for example) | |
def mock_env(partial_env_hash) | |
old_env = ENV.to_hash | |
ENV.update partial_env_hash | |
begin | |
yield | |
ensure | |
ENV.replace old_env | |
end | |
end | |
# usage | |
mock_env('MY_ENV_VAR' => 'Hello') do | |
assert something? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment