Skip to content

Instantly share code, notes, and snippets.

@odlp
Last active October 27, 2021 16:32
Show Gist options
  • Save odlp/40f83c8d1e394d97b558dde606d4bbb7 to your computer and use it in GitHub Desktop.
Save odlp/40f83c8d1e394d97b558dde606d4bbb7 to your computer and use it in GitHub Desktop.
Modifying the test environment temporarily
module EnvironmentHelper
def with_environment(replacement_env)
original_env = ENV.to_hash
ENV.update(replacement_env)
yield
ensure
ENV.replace(original_env)
end
end
include EnvironmentHelper
puts "Before: #{ENV.fetch("FOO")}"
with_environment("FOO" => "baz") do
puts "During: #{ENV.fetch("FOO")}"
end
puts "After: #{ENV.fetch("FOO")}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment