1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
# frozen_string_literal: true
#
# ENV stubbing is opt-in, via a shared context, rather than global
#
# Usage:
#
# describe 'my stubbed test' do
# include_context 'with stubbed env'
# before do
# stub_env('FOO' => 'is bar')
# end
# it 'does a thing' do
# expect(ENV['FOO']).to eq('is bar')
# end
# end
#
module RSpec
module StubbedEnv
# Helpers to unobtrusively stub ENV
module TestHelpers
# Can be called with all key value pairs to be stubbed as a hash:
#
# stub_env('A' => 'B', 'C' => 'D', 'E' => 'F')
#
# Alternatively can be called one per ENV key-value pair to stub:
#
# stub_env('A', 'B')
# stub_env('C', 'D')
# stub_env('E', 'F')
def stub_env(key_or_hash, value = nil)
init_stub unless env_stubbed?
if key_or_hash.is_a? Hash
key_or_hash.each { |k, v| add_stubbed_value(k, v) }
else
add_stubbed_value key_or_hash, value
end
end
private
STUBBED_KEY = '__STUBBED__'
def add_stubbed_value(key, value)
allow(ENV).to receive(:[]).with(key).and_return(value)
allow(ENV).to receive(:fetch).with(key).and_return(value)
allow(ENV).to receive(:fetch).with(key, anything) do |_, default_val|
value || default_val
end
end
def env_stubbed?
ENV[STUBBED_KEY]
end
def init_stub
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:fetch).and_call_original
add_stubbed_value(STUBBED_KEY, true)
end
end
end
end
|