对模型属性进行存根

我正在为我的汽车课写rspec测试,并且有关于设置模拟的问题。 我想在汽车中存放零件arrays,我该怎么做?

我有以下代码:

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 

假设您使用RSpec进行模拟而不是另一个框架:

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