expect(obj).to receive(:method) not working

20 views
Skip to first unread message

Jack Royal-Gordon

unread,
Apr 13, 2020, 12:19:42 AM4/13/20
to rs...@googlegroups.com
Consider the following controller test fragment:


it 'should call the kill function on the user' do
byebug
expect(@user1).to receive(:kill)
put :update, id: :user_kill, user_id: @user1.id
end


At the breakpoint in byebug, I set a breakpoint at "User#kill" and display the value "@user1.id". When the program breaks at User#kill, I view “self.id” — same value. I then continue and the est fails with the message:

Failure/Error: expect(@user1).to receive(:kill)
(#<User:0x007fc38cd6a248>).kill(any args)
expected: 1 time with any arguments
received: 0 times with any arguments

If I put another breakpoint after the “put” command, I can verify that the record has been deleted (which is the effect of calling User#kill. I can test the effect instead of testing the method call directly, but I’d rather do it this way (I have several other functions whose effect is less easy to detect, therefore I’d like to get this style of test to work). Any thoughts?

If it makes a difference, running RSpec 2.99 on Rails 3.2.21. (Please don’t advise me to upgrade, I’m adding tests preparatory to upgrading.)

Phil Pirozhkov

unread,
Apr 13, 2020, 12:34:04 AM4/13/20
to Jack Royal-Gordon
It doesn't work that way, you have to make an expectation on the side effect of that action, as the object you are making the 'receive' expectation on is different from the object that will be returned by 'find' in the controller. You can check '.object_id' to make sure.

I suggest you to check that the number of alive users has changed.

Jack Royal-Gordon

unread,
Apr 13, 2020, 3:20:19 AM4/13/20
to rs...@googlegroups.com
Thanks, Phil. The documentation seemed to be a little ambiguous on the subject.

> On Apr 12, 2020, at 9:34 PM, Phil Pirozhkov <pirj...@gmail.com> wrote:
>
> It doesn't work that way, you have to make an expectation on the side effect of that action, as the object you are making the 'receive' expectation on is different from the object that will be returned by 'find' in the controller. You can check '.object_id' to make sure.
>
> I suggest you to check that the number of alive users has changed.
>
> --
> You received this message because you are subscribed to the Google Groups "rspec" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/20200413043400.5840974.54103.1866%40gmail.com.

Chris Irish

unread,
Apr 13, 2020, 5:42:33 PM4/13/20
to rspec
You could also stub the .find in your controller and force the object in the action to be the double from your test

i.e.  expect(User).to receive(:find).and_return(@user1)

then your test would work assume kill was actually called on @user1

Jack Royal-Gordon

unread,
Apr 13, 2020, 7:17:03 PM4/13/20
to rs...@googlegroups.com
Thanks, Chris. That did the trick!

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages