Ryan-Neal Mes
Ryan-Neal Mes

Reputation: 6263

rspec receive method not working as expected

I can't understand why the following code does not work in rspec when running my tests.

Lead.update_status(2,2,true)
expect(Lead).to receive(:update_status).with(2, 2, true)

I have also tried

Lead.update_status(2,2,true)
expect_any_instance_of(Lead).to receive(:update_status).with(2, 2, true)

The error I I get is

(<Lead(id: integer, contact_id: integer, course_presentation_id: integer, status: integer, created_by_user_id: integer, updated_by_user_id: integer, created_at: datetime, updated_at: datetime, mailchimp_status: integer) (class)>).update_status(2, 2, true)
           expected: 1 time with arguments: (2, 2, true)
           received: 0 times with arguments: (2, 2, true)

Note if I add puts "test" into my code this is prints out, so I know that update_status is actually working.

Any ideas? Thanks.

Upvotes: 14

Views: 6492

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

You should place your expectation before the code you expect to call that method:

expect(Lead).to receive(:update_status).with(2, 2, true)
Lead.update_status(2,2,true)

Upvotes: 35

Related Questions