module EnvTestHelpers

Constants

VERSION

Public Instance Methods

mock_env_vars(vars) click to toggle source

describe Thing do

before(:all) do
  mock_env_vars('FOO' => 'bar')
end
it 'does something with the FOO environment variable' do
  # logic that depends upon ENV['FOO'] goes here
end

end

# File lib/env_test_helpers.rb, line 26
def mock_env_vars(vars)
  vars.each do |k, v|
    allow(ENV).to receive(:[]).with(k).and_return(v)
  end
end
with_env_vars(vars) { || ... } click to toggle source

it ‘does something with the FOO environment variable’ do

with_env_vars 'FOO' => 'bar' do
  # logic that depends upon ENV['FOO'] goes here
end

end

# File lib/env_test_helpers.rb, line 9
def with_env_vars(vars)
  original = ENV.to_hash
  vars.each { |k, v| ENV[k] = v }
  begin
    yield
  ensure
    ENV.replace(original)
  end
end