[mocha-developer] Testing a call to super

Jonathan Leighton j at jonathanleighton.com
Tue Jul 3 15:30:53 EDT 2007

> I can't think of a simple way offhand. Can you give an example (both test &
> application code) to explain why you want to do it?

Sorry for my slow reply to this, I've been pretty busy.

Sometimes you might have a concrete subclass of an abstract class, and
some functionality which the abstract class provides must happen in
order for the method to be "successful". In this case the concrete class
will be buggy if it doesn't call "super" in that method (or re-implement
the same code provided by the abstract class).

The reason I was asking for this is because I am a developer on this
project: http://nex3.leeweiz.net/posts/3 (we don't have a website yet).

Each "thing" on the canvas like a rectangle, circle, line, curve, etc is
a "Drawable" (the abstract class). Each Drawable has a state but doesn't
actually appear until its "draw" method is called. Each Drawable also
has a clone of the "brush" when it was created. This is how it knows
what fill/stroke colour etc. to use.

Basically, in order to draw a Drawable, the Drawable's brush must also
be drawn. Every single Drawable does this so the abstract class has the
code for Drawing the brush, and the concrete classes call "super" to
make this happen. If super were not called the Drawable wouldn't draw
correctly and the code would be broken.

I guess I envisaged something like


Maybe this isn't a problem for Mocha though. It occurred to me that this
kinda thing is a perfect use for RSpec's it_should_behave_like, which
could test the brush painting whilst preventing duplication of code. As
a bonus, it_should_behave_like would (correctly) let the test pass if
"super" wasn't called, but the superclass' code was re-implemented.


Jonathan Leighton, Web Developer

More information about the mocha-developer mailing list