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?
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://twitter.com/ed_howland
On Jul 13, 2010, at 11:29 AM, Benyi W. wrote:
because is_a? may not be called at all, it just like a stub. Is my understanding correct?
Yes.
Is there any guide how to use stub and message expectation?
any_number_of_times was introduced before we introduced stubs, way back,
way back. I use stub() rather than should_receive + any_number_of_times.
HTH,
David