James Mead
2008-Jan-20 17:11 UTC
[mocha-developer] Bug #17118 - expectations should take precedence over stubs
I wanted to draw attention to this bug report [A] which highlights a change that was made between Mocha 0.4 and 0.5. It may have lead to tests which pass unexpectedly. Does my explanation (below) make sense to people? It feels like we should at least add some warnings to the documentation. You are correct that this behaviour did change between Mocha v0.4.0 and> v0.5.0 (in revision 115). > > The idea of this change was to bring Mocha into line with jMock v1 [1] so > that it stops matching expectations when the maximum number of expected > invocations is reached. You are suffering from "gotcha" number 2 in that > jMock reference: "if you create a stub and then an expectation for the same > method, the expectation will match, and when it stops matching the stub will > be used instead, possibly masking test failures". In your example, the > "never" expectation will never match because it has already reached its > maximum number of expected invocations, but Mocha will then find the stub > which does match. > > Personally I prefer not to use setup methods, I prefer to make tests as > self-contained as possible [2]. I find this makes tests more maintainable > (albeit at the expense of some duplication). So in this case I would prefer > to *explicitly* put the stub expectation in every test that needed it. If > setting up the stub expectation was complex or multiple stub expectations > were needed I would extract that into a descriptive method and call it > explicitly from each test that needed it. > > If you don''t like this approach, you do have another alternative. Using > "edge" Mocha (i.e. revision 233 or later), you should be able to use the > state-machine feature to achieve your goal [2]. This should be released as a > gem soon. > > Thanks, James. > http://blog.floehopper.org > > [1] "Stubs, Expectations and the Dispatch of Mocked Methods" > http://www.jmock.org/jmock1-dispatch.html > [2] "Testing: Inline Setup" > http://blog.jayfields.com/2007/06/testing-inline-setup.html > [3] OverrideExpectationsFromSetUpAcceptanceTest > http://pastie.caboo.se/141188 >-- James. http://blog.floehopper.org [A] http://tinyurl.com/29ba6d
James Mead
2008-Jan-21 15:21 UTC
[mocha-developer] Bug #17118 - expectations should take precedence over stubs
FYI - a new documentation page has been added for jMock2 "Overriding Expectations Defined in the Test Set-Up" [1]. -- James. http://blog.floehopper.org [1] http://www.jmock.org/override.html
James Mead
2008-Jan-21 17:34 UTC
[mocha-developer] Bug #17118 - expectations should take precedence over stubs
Sorry Dan - for some reason MailMan''s content filtering didn''t like your signature attachment or something... ---------- Forwarded message ---------- From: Dan North <dan at tastapod.com> To: mocha-developer at rubyforge.org Date: Sun, 20 Jan 2008 21:22:00 +0000 Subject: Re: [mocha-developer] Bug #17118 - expectations should take precedence over stubs Hi James. I agree in principle but disagree in this specific case (the never() expectation). For my two penn''orth, never() should both match and fail fast if an invocation matches. It shouldn''t matter whether or not you have a stub (so stubbing in a setup method would be irrelevant in this case, but it''s still icky). Cheers, Dan -- James. http://blog.floehopper.org http://tumble.floehopper.org