Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active July 26, 2023 14:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrik/8f4a9fc07e42a3a410a1049af2631bd9 to your computer and use it in GitHub Desktop.
Save henrik/8f4a9fc07e42a3a410a1049af2631bd9 to your computer and use it in GitHub Desktop.
Stub ENVs in RSpec.
def stub_env(key, value)
is_set_up_flag = "__stub_env_is_set_up"
unless ENV[is_set_up_flag]
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:[]).with(is_set_up_flag).and_return(true)
end
allow(ENV).to receive(:fetch).with(key).and_return(value)
allow(ENV).to receive(:[]).with(key).and_return(value)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment