重新加载在RSpec中不起作用的对象 [英] Reloading an object not working in rspec
本文介绍了重新加载在RSpec中不起作用的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码测试控制器方法:
I am trying to test a controller method with the following code:
it "should set an approved_at date and email the campaign's client" do
@campaign = Campaign.create(valid_attributes)
post :approve, id: @campaign.id.to_s
@campaign.reload
@campaign.approved_at.should_not be(nil)
end
但是,何时我运行此测试,得到以下错误:
However, when I run this test, I get the following error:
Failure/Error: @campaign.reload
ActiveRecord::RecordNotFound:
Couldn't find Campaign without an ID
当我在Rails控制台,重新加载工作,并且值设置为我需要的值。在rspec测试中运行代码时,为什么重新加载对我不起作用?
When I run the analagous lines in the rails console, the reload works and the value is set as I need it to be. Why isn't reload working for me when I run the code in an rspec test?
推荐答案
我通过切换到FactoryGirl:
I solved the problem by switching to FactoryGirl:
@campaign = FactoryGirl.create(:pending_approval_campaign)
@campaign.approved_at.should be(nil)
post :approve, id: @campaign.id.to_s
@campaign.reload
@campaign.approved_at.should_not be(nil)
可以按预期工作
这篇关于重新加载在RSpec中不起作用的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文