Rspec, should_receive, called twice in controller with different arguments, how to test?

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?



What’s the error you’re getting?

On Aug 6, 2008, at 11:21 PM, Ryan B. 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.



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.