2010-10-04 86 views
0

我使用factory_girl + rspec的on Rails的2-3个稳定的属性:磕碰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的工作方式,并且首选直接设置它。

任何想法为什么?

回答

3

您需要存根read_attribute方法

before(:each) do 
    @profile.stub!(:read_attribute).with(:lastfm_enabled).and_return(true) 
    end