开发者

Stubbing A Model Attribute

I am writing rspec test for my Cars class, and have a question regarding setting up mocks. I'd like to stub the parts array in Cars, how can i do that?

I have the following code:

class Cars
  has_many :parts

  def heavy_count
    parts.inject(0) { |sum, v开发者_开发技巧| v.weight > 10 ? sum + 1 : sum }
  end
end

With test

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


Assuming you're using RSpec for mocking and not another framework:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜