On Monday, February 11, 2013 3:58:21 PM UTC-8, clay s wrote:

> On Monday, February 11, 2013 1:00:25 PM UTC-8, [email protected] wrote:
>
>> x.should_receive(:foo).and_return(:a_reasonable_value)
>>
>
> If the code will fail without specifying that return value, then you 
> didn't need to mock in the first place.
>

This is often true, but not universally.  Overall, I consider the use of 
`should_receive` with a return value to be a code smell since I generally 
care about command/query separation. That said, there are still cases where 
it makes sense to use `should_receive` with a return value.  For example, 
consider the case where you are adding some caching optimizations to an 
object that delegates expensive work to a collaborator.  To test drive 
this, you may want to specify that a particular message is sent to the 
collaborator only once, and `should_receive(:message).once` is a fine way 
to do that.  If the object cares about the return value and does not expect 
it to ever be nil, you'll need to specify a return value as well.

If using `should_receive(:foo).and_return(value)` bothers you that much, 
then feel free not to use it.  I think you're instincts that it's often not 
needed (and that confusing stubs with mocks is bad) are correct.  But there 
are still cases where it's useful, and RSpec provides it.

Myron

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msg/rspec/-/iV5zfpIMJYMJ.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to