2010-10-04 82 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