[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:
http://pastie.org/1043160
]

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)
./mock_spec.rb:61:

for stubs

vs.
)
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)
./mock_spec.rb:28:

for mocks.

The web page says:
"Explicitly Imprecise Counts

my_mock.should_receive(:msg).any_number_of_times
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.


Cheers,
Ed

Ed Howland
http://greenprogrammer.wordpress.com
http://twitter.com/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