Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active February 28, 2023 14:35
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 henrik/6549ee48ebff4407b8dc0815f985d90c to your computer and use it in GitHub Desktop.
Save henrik/6549ee48ebff4407b8dc0815f985d90c to your computer and use it in GitHub Desktop.
Climate Control RSpec helper https://github.com/thoughtbot/climate_control
module ClimateControlHelpers
# Usage:
#
# describe Foo do
# stub_envs(
# FOO: "bar",
# )
#
# it "uses the ENVs"
# end
#
# Note that the hash values are evaluated at file-loading time.
# If it needs to be dynamic, use a block:
#
# describe Foo do
# let(:bar) { Time.now }
#
# stub_envs do
# {
# FOO: bar,
# }
# end
#
# it "uses the ENVs"
# end
#
def stub_envs(hash = {}, &)
# This uses https://github.com/thoughtbot/climate_control.
# When we used `allow(ENV)` we got intermittent errors, perhaps due to threading issues.
around do |example|
hash = block_given? ? instance_eval(&) : hash
ClimateControl.modify(hash) { example.run }
end
end
end
RSpec.configure do |config|
config.extend(ClimateControlHelpers)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment