Lee
Lee

Reputation: 9462

Rails: problem setting expectations on mock model in RSpec

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.

Upvotes: 3

Views: 1762

Answers (2)

Lille
Lille

Reputation: 61

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

Upvotes: 1

Greg Campbell
Greg Campbell

Reputation: 15292

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)

Upvotes: 10

Related Questions