[mocha-developer] how does Mocha compare in terms of classical vs mock-based testing, and stubbing???

James Mead jamesmead44 at gmail.com
Thu Feb 8 06:24:18 EST 2007


On 08/02/07, Greg Hauptmann <greg.hauptmann.ruby at gmail.com> wrote:
>
> I did read this, but re-read briefly again.....arrr....by interface
> discovery you were talking about the development concept behind the mock
> object TDD concept.   I was on the lookout for some aspect of Mocha that
> was
> going to do a "reflection" style dynamic interface discovery and then
> build
> the mock object from this.  Got it now, so with the Mock object approach
> the
> concept is that you haven't written the code for the collaborators yet.
> Let me know if I'm wrong  :)


Yes. Using TDD with mock objects makes you think about what behaviour does
the object under test need from its collaborator(s). This drives out the
public method signatures for the collaborator(s) (i.e. interface discovery),
but allows you to focus on the object under test and not immediately worry
about how to implement the behaviour of the collaborators.

The only negative I see re potential use of Mocha to solve my "need to stub
> out paypal for local testing" is that Mocha seems to extend test cases no?
> i.e. this would allow me to unit test my components with paypal stubbed
> out
> in the form of mock object, HOWEVER if I just wanted to do ad hoc
> testing/use of my web application (i.e. not driven by a test case) then
> I'll
> still need a separate "test paypal layer" (or stub out paypal in other
> words).   If I've still missed something I'd be keen to know.
>

The latest version of Mocha is not tightly coupled to Test::Unit. In theory
you could use Mocha to stub out the PayPal service in your development
environment without needing a TestCase. However, I think you'd be better off
using something more like a standard Rails "mock" in the test/mocks
directory (what Martin Fowler calls a Fake Object - see
http://www.martinfowler.com/articles/mocksArentStubs.html#TheDifferenceBetweenMocksAndStubs).
i.e. have an alternative implementation of the PayPal service which replaces
the real implementation and returns canned results.

-- 
James.
http://blog.floehopper.org


More information about the mocha-developer mailing list