[英]stubbing factory_girl + rspec methods and attributes
我在Rails 2-3-stable上使用factory_girl + rspec:
测试:
context "test" do
before(:each) do
@profile.stub!(:lastfm_enabled?).and_return(true)
end
it "should be able to scrobble if true" do
@profile.update_attribute(:lastfm_scrobbling, true)
@profile.lastfm_scrobbling_enabled?.should be_true
end
end
码:
def lastfm_scrobbling_enabled?
lastfm_enabled? && lastfm_scrobbling
end
这个简单的测试失败,加上真正的lastfm_enabled? 方法被称为真正的。
@profile不是模拟,它是使用factory_girl创建的。
最初,我还添加了lastfm_scrobbling属性,但是我正在考虑factory_girl的工作方式,因此希望直接设置它。
知道为什么吗?
您需要存根read_attribute方法
before(:each) do
@profile.stub!(:read_attribute).with(:lastfm_enabled).and_return(true)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.