Last active
February 28, 2023 14:35
-
-
Save henrik/6549ee48ebff4407b8dc0815f985d90c to your computer and use it in GitHub Desktop.
Climate Control RSpec helper https://github.com/thoughtbot/climate_control
This file contains 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
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