Skip to content

Instantly share code, notes, and snippets.

@BrianSigafoos
Forked from jazzytomato/mock_env.rb
Created August 23, 2021 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianSigafoos/47df273009c49842b8b7a09e2742bcff to your computer and use it in GitHub Desktop.
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
# 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