Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stubbing key/value pair in ruby on rails ENV

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?

like image 450
Obromios Avatar asked Mar 13 '23 08:03

Obromios


1 Answers

You can use

stub_const 'ENV', ENV.to_h.merge('ADWORDS_RUN' => 'No')
like image 199
Mori Avatar answered Mar 21 '23 03:03

Mori