Benyi Wang
2010-Jul-13 16:29 UTC
[rspec-users] [Rspec] Difference between stub and message expectation with any_number_of_times
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? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100713/94a480e3/attachment.html>
Ed Howland
2010-Jul-13 22:14 UTC
[rspec-users] [Rspec] Difference between stub and message expectation with any_number_of_times
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 >
David Chelimsky
2010-Jul-13 23:04 UTC
[rspec-users] [Rspec] Difference between stub and message expectation with any_number_of_times
On Jul 13, 2010, at 11:29 AM, Benyi Wang 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?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