我正在使用 rspec 编写单元测试。
我想模拟 Rails.env.development?返回 true。我怎样才能做到这一点?
我试过这个
Rails.env.stub(:development?, nil).and_return(true)
它抛出这个错误
activesupport-4.0.0/lib/active_support/string_inquirer.rb:22:in `method_missing': undefined method `any_instance' for "test":ActiveSupport::StringInquirer (NoMethodError)
更新
红宝石版本 ruby-2.0.0-p353,
导轨 4.0.0,
规范2.11
describe "welcome_signup" do
let(:mail) { Notifier.welcome_signup user }
describe "in dev mode" do
Rails.env.stub(:development?, nil).and_return(true)
let(:mail) { Notifier.welcome_signup user }
it "send an email to" do
expect(mail.to).to eq([GlobalConstants::DEV_EMAIL_ADDRESS])
end
end
end
这里描述了一种更好的方法:https://stackoverflow.com/a/24052647/362378 https://stackoverflow.com/a/24052647/362378
it "should do something specific for production" do
allow(Rails).to receive(:env) { "production".inquiry }
#other assertions
end
这将提供所有功能,例如Rails.env.test?
如果您只是比较字符串,也可以使用Rails.env == 'production'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)