Last active
August 29, 2024 13:18
-
-
Save jazzytomato/79bb6ff516d93486df4e14169f4426af 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.to_hash | |
ENV.update partial_env_hash | |
begin | |
yield | |
ensure | |
ENV.replace old | |
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
Nice, I just modified it a bit so I can test methods that use
Rails.env.production?
etc and added defaults.