Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 581
Ryan-Neal Mes Avatar asked Mar 20 '14 11:03

Ryan-Neal Mes


1 Answers

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)
like image 105
Marek Lipka Avatar answered Oct 09 '22 10:10

Marek Lipka