ssi*_*lla 25 rspec rspec2 rspec-rails
我正在使用rspec编写单元测试.
我想模仿Rails.env.develepment?返回真实.我怎么能实现这个目标?
我试过这个
Rails.env.stub(:development?, nil).and_return(true)
Run Code Online (Sandbox Code Playgroud)
它抛出了这个错误
activesupport-4.0.0/lib/active_support/string_inquirer.rb:22:in `method_missing': undefined method `any_instance' for "test":ActiveSupport::StringInquirer (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
更新ruby版本ruby-2.0.0-p353,rails 4.0.0,rspec 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
Run Code Online (Sandbox Code Playgroud)
iGE*_*GEL 57
这里有一个更好的方法:https://stackoverflow.com/a/24052647/362378
it "should do something specific for production" do
allow(Rails).to receive(:env) { "production".inquiry }
#other assertions
end
Run Code Online (Sandbox Code Playgroud)
这将提供所有功能Rails.env.test?,如果您只是比较字符串,也可以工作Rails.env == 'production'
got*_*tva 16
你应该在存根it,let,before块.将代码移到那里就可以了
这段代码可以在我的测试中使用(也许你的变体可以正常工作)
Rails.env.stub(:development? => true)
Run Code Online (Sandbox Code Playgroud)
例如
describe "in dev mode" do
let(:mail) { Notifier.welcome_signup user }
before { Rails.env.stub(:development? => true) }
it "send an email to" do
expect(mail.to).to eq([GlobalConstants::DEV_EMAIL_ADDRESS])
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10269 次 |
| 最近记录: |