[rspec-users] [Rspec] Difference between stub and message expectation with any_number_of_times

Ed Howland ed.howland at gmail.com
Tue Jul 13 18:14:54 EDT 2010

Ok, here is my stab at this:

Since be seem to be only setting and testing bare mocks, I'd assume
this is just useful to pass the mock as a stand in for some other
object. So running a test: [See the code at pastie:

I get subtly different messages:

Spec::Mocks::MockExpectationError in 'mocking stub should not be ok if
it gets a at least one good argument and an unexpected argument'
Mock "mymock" received unexpected message :is_a? with (NoMock)

for stubs

Spec::Mocks::MockExpectationError in 'mocking mock should see the
incorrect one and report an error'
Mock "mymock" received :is_a? with unexpected arguments
  expected: (MyClass)
       got: (NoMock)

for mocks.

The web page says:
"Explicitly Imprecise Counts

The message can be received 0 or more times.

I can't envision a use case where this is needed, or can't be emulated
via a stub.

I'd be interested in David C's take or someone else's.


Ed Howland

On Tue, Jul 13, 2010 at 12:29 PM, Benyi Wang <bewang.tech at gmail.com> wrote:
> I'm wondering what's the difference between stub and message expectation
> with any_number_of_times, for example:
> myMock = mock("mymock")
> myMock.stub!(:is_a?).with(MyClass).and_return(false)
> and
> myMock = mock("mymock")
> myMock.should_receive(:is_a?).with(MyClass).any_number_of_times.and_return(false)
> because is_a? may not be called at all, it just like a stub. Is my
> understanding correct?
> Is there any guide how to use stub and message expectation?
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list