module RSpec::StubbedEnv::TestHelpers
Helpers to unobtrusively stub ENV
Constants
- STUBBED_KEY
Public Instance Methods
stub_env(key_or_hash, value = nil)
click to toggle source
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')
# File lib/rspec/stubbed_env/test_helpers.rb, line 31 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 Instance Methods
add_stubbed_value(key, value)
click to toggle source
# File lib/rspec/stubbed_env/test_helpers.rb, line 44 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
env_stubbed?()
click to toggle source
# File lib/rspec/stubbed_env/test_helpers.rb, line 52 def env_stubbed? ENV[STUBBED_KEY] end
init_stub()
click to toggle source
# File lib/rspec/stubbed_env/test_helpers.rb, line 56 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