开发者

Stubbing A Model Attribute

开发者 https://www.devze.com 2023-01-08 22:19 出处:网络
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 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

精彩评论

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

关注公众号