I want to test the effect of the value of an ENV key on my code. I am stubbing this by using
allow(ENV).to receive(:[]).with('ADWORDS_RUN').and_return('No')
This was working until I changed the target code to include accessing another ENV key. The target code now includes the following
def not_local_machine?
!ENV['LOCAL_MACHINE']
end
The test now fails in the above function with the error message
Failure/Error: get 'home'
ENV received :[] with unexpected arguments
expected: ("ADWORDS_RUN")
got: ("LOCAL_MACHINE")
Please stub a default value first if message might be received with other args as well.
It appears that my current method of stubbing is wiping out other ENV keys. How do I stub an ENV key to avoid this problem?
You can use
stub_const 'ENV', ENV.to_h.merge('ADWORDS_RUN' => 'No')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With