# Surprised by stub/mock interaction

**URL:** <https://www.ruby-forum.com/t/surprised-by-stub-mock-interaction/161339>\
**Category:** RSpec\
**Created:** [February 25, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/surprised-by-stub-mock-interaction/161339 "2009-02-25T17:18:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [February 25, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/surprised-by-stub-mock-interaction/161339/1 "2009-02-25T17:18:45Z")

</div>

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-rspec-stubs-and-mocks](http://blog.davidchelimsky.net/2006/11/9/tutorial-rspec-stubs-and-mocks)).

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

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [February 25, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/surprised-by-stub-mock-interaction/161339/2 "2009-02-25T17:38:52Z")

</div>

On Wed, Feb 25, 2009 at 10:10 AM, Jeff T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [February 25, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/surprised-by-stub-mock-interaction/161339/3 "2009-02-25T18:51:30Z")

</div>

On Wed, Feb 25, 2009 at 10:23 AM, David C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [February 25, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/surprised-by-stub-mock-interaction/161339/4 "2009-02-25T19:31:04Z")

</div>

On Wed, Feb 25, 2009 at 10:39 AM, Jeff T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.
