Forum: Ruby on Rails Rspec, should_receive, called twice in controller with different arguments, how to test?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Philip Hallstrom (Guest)
on 2008-08-07 08:03
(Received via mailing list)
Hi -

I've got a simple controller.  It calls Foo.something('xyz') and
returns 'XYZ'.  I have a before filter for my entire application that
calls Foo.something(nil) and returns nil.

I'm not sure how to specify that in my tests and rspec is complaining
about expecting one or the other.  I'm sure it's something simple, but
I'm missing it.

What I want is this, but in the additive sense and it seems to be in
the replacement sense:


What's the syntax to handle this?


9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2008-08-07 08:22
(Received via mailing list)
What's the error you're getting?
Philip Hallstrom (Guest)
on 2008-08-07 18:19
(Received via mailing list)
On Aug 6, 2008, at 11:21 PM, Ryan Bigg wrote:

> What's the error you're getting?

Mock 'Class' expected :find_by_token with ("token_for_philip") but
received it with (nil)

That's when i had just the one should_receive (for the call inside the

When I added the call/should_receive to the before_filter (typically
called with nil) I then get this:

Mock 'Class' expected :find_by_token with (nil) once, but received it

If I don't call the before_filter for that action it all works like
you'd expect.

5030981121b21bed8aee074f68bd5074?d=identicon&s=25 RSL ___ (rsl)
on 2008-08-08 13:44
(Received via mailing list)

you have a couple of options. stub out the before_filter completely in
your before block [or in the example i guess] or put all the arguments
[or at least some of the defining ones] in the should_receive. i'd
probably do the former.

also you shouldn't be should_receiving the filter [because that's
probably going on in the before block] and should be stubbing it
instead. should_receive in the before block will create two
expectations for every spec and that's usually a bad idea. hope that


PS: reading your error message it might be possible that whatever
argument you're sending as the with evaluates to nil. since it's
saying it receives with nil twice.
This topic is locked and can not be replied to.