change rails environment in the mid of testing

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

How to change the environment variable of rails in testing

5条回答
  •  孤独总比滥情好
    2020-12-24 02:01

    Sometimes returning a different environment variable can be a headache (required production environment variables, warning messages, etc).

    Depending on your case, as an alternate you may be able to simply return the value you need for your test to think it's in another environment. Such as if you wanted Rails to believe it is in production for code that checks Rails.env.production? you could do something like this:

    it "does something specific when in production" do 
      allow(Rails.env).to receive(:production?).and_return(true)
      ##other assertions
    end
    

    You could do the same for other environments, such as :development?, :staging?, etc. If you don't need the full capacity of returning a full environment, this could be another option.

提交回复
热议问题