存根factory_girl + rspec 方法和属性
我在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的工作方式并更愿意直接设置它。
知道为什么吗?
I'm using factory_girl + rspec on Rails 2-3-stable:
Test:
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
Code:
def lastfm_scrobbling_enabled?
lastfm_enabled? && lastfm_scrobbling
end
This simple test fails, plus the real lastfm_enabled? method gets called for real.
@profile is not a mock, it's created with factory_girl.
Initially I also stubbed the lastfm_scrobbling attribute, but I was thinking about the way factory_girl works and preferred to set it directly.
Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要存根 read_attribute 方法
You need stub the read_attribute method