views:

222

answers:

2

I am trying to set expectations on a mocked ActiveRecord model. I have created the following example, which should pass based on the documentation I can find.

it "should pass given the correct expectations" do
  payment = mock_model(Payment)
  payment.should_receive(:membership_id).with(12)
  payment.membership_id = 12
end

It is failing with the error "...Mock 'Payment_1004' received unexpected message :membership_id= with (12)"

I realize I am testing the mocking framework, I am just trying to understand how to setup expectations.

+3  A: 

You're setting the expectation on the wrong method name - :membership_id is the "getter", :membership_id= is the "setter". The correct line would be:

payment.should_receive(:membership_id=).with(12)
Greg Campbell
Perfect, thanks!
Lee
A: 

Another useful "out" here -- if one doesn't care about the id key -- is to do something like the following:

mock_model(Payment,:[]= => nil, :save=> nil)

...or maybe just

mock_model(Payment,:[]= => nil)

Lille

Lille