2010-07-27 45 views
1

我正在爲我的Cars類寫rspec測試,並且有關於設置mock的問題。我想在Cars中存儲零件數組,我該怎麼做?Stubbing A Model Attribute

我有以下代碼:

class Cars 
    has_many :parts 

    def heavy_count 
    parts.inject(0) { |sum, v| v.weight > 10 ? sum + 1 : sum } 
    end 
end 

隨着測試

context ("#heavy_count") do 
    let(:car) {mock_model(Car, :brand => "toyota")} 
    let(:vote_1) {mock_model(Part, :weight => 11)} 
    let(:vote_2) {mock_model(Part, :weight => 11)} 

    it "should return 2 if there are 2 parts heavier than 10" do 
    #how do I stub parts here? 
    end 
end 

回答

4

假設你正在使用RSpec的嘲諷爲,而不是另一個框架:

Part.should_receive(:find).and_return([vote_1, vote2])