change rails environment in the mid of testing

后端 未结 5 1905
天命终不由人
天命终不由人 2020-12-24 01:11

How to change the environment variable of rails in testing

5条回答
  •  隐瞒了意图╮
    2020-12-24 01:54

    As a simpler variation on several answers above, this is working for me:

    allow(Rails).to receive(:env).and_return('production')
    

    Or, for as I'm doing in shared_examples, pass that in a variable:

    allow(Rails).to receive(:env).and_return(target_env)
    

    I suspect this falls short of the ...StringInquirer... solution as your app uses additional methods to inspect the environment (e.g. env.production?, but if you code just asks for Rails.env, this is a lot more readable. YMMV.

提交回复
热议问题