Forum: RSpec Surprised by stub/mock interaction

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5dade05c7d95ee88f2d6d8970bb2071e?d=identicon&s=25 Jeff Talbot (Guest)
on 2009-02-25 17:18
(Received via mailing list)
Hello,

Something I do often is use stub methods in before blocks and mock
expectations in a specific examples (much like described here:
http://blog.davidchelimsky.net/2006/11/9/tutorial-...).

I was just surprised with an instance of doing this and I thought I'd
check
with the group to see if I shouldn't have been.

What should be the expected output of the following -- assume it's the
only
code in a spec file:

  class Foo; def bar; end; end

  it "should print something" do
    foo = Foo.new
    foo.stub!(:bar).and_return(true)
    foo.should_receive(:bar).at_least(:once).and_return(false)
    puts foo.bar
    puts foo.bar
  end

I expected "false / false". The actual output is "false / true".

Jeff
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-02-25 17:38
(Received via mailing list)
On Wed, Feb 25, 2009 at 10:10 AM, Jeff Talbot <jeff.a.talbot@gmail.com>
wrote:
> code in a spec file:
>
> I expected "false / false". The actual output is "false / true".

"false / true" is correct.

The first call to foo.bar satisfies the message expectation
(should_receive), so the message expectation is no longer paying
attention after that. If there was no stub, it would field any
subsequent calls, but in this case the stub gets it.

FWIW, I'd avoid mixing stub! and should_receive for cases like this,
just to avoid the sort of confusion you're experiencing.

Cheers,
David
5dade05c7d95ee88f2d6d8970bb2071e?d=identicon&s=25 Jeff Talbot (Guest)
on 2009-02-25 18:51
(Received via mailing list)
On Wed, Feb 25, 2009 at 10:23 AM, David Chelimsky
<dchelimsky@gmail.com>wrote:

> check
> >     foo.stub!(:bar).and_return(true)
> (should_receive), so the message expectation is no longer paying
> attention after that. If there was no stub, it would field any
> subsequent calls, but in this case the stub gets it.
>

Makes sense, and after thinking about it some more I think I'm fully
satisfied with that explanation.


> FWIW, I'd avoid mixing stub! and should_receive for cases like this,
> just to avoid the sort of confusion you're experiencing.
>

By "cases like this" I assume you mean cases where the method is invoke
multiple times?

Jeff
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-02-25 19:31
(Received via mailing list)
On Wed, Feb 25, 2009 at 10:39 AM, Jeff Talbot <jeff.a.talbot@gmail.com>
wrote:
>> > expectations in a specific examples (much like described here:
>> >
>> > I expected "false / false". The actual output is "false / true".
>
>>
>> FWIW, I'd avoid mixing stub! and should_receive for cases like this,
>> just to avoid the sort of confusion you're experiencing.
>
> By "cases like this" I assume you mean cases where the method is invoke
> multiple times?

Yep. I'd avoid the stub in this case.
This topic is locked and can not be replied to.