[mocha-developer] Stubbing objects and calling to_json on them errors (circular reference)

John D. Hume duelin.markers at gmail.com
Fri Oct 24 08:49:09 EDT 2008

On Mon, Oct 20, 2008 at 3:10 PM, Bill Kocik <bkocik at gmail.com> wrote:
> Our first step in the cleanup effort is to
> try to increase our test coverage to a level we're comfortable with, so for
> now I'm stuck with writing less-than-ideal tests for less-than-ideal code.

I realize it's been a few days and apologize for the late response,
but I just saw this. I hate to be the guy on the mailing list saying
"you shouldn't be trying to do what you're trying to do," but that's
sort of where I'm headed. I hope it's helpful anyway.

When adopting a codebase that wasn't developed test-driven, I'd
strongly recommend focusing on Rails-style functional tests rather
than introducing mock-based testing of the current design. The biggest
downside to mocking is that it ties you to certain interactions
between objects. When you're using it to determine what those
interactions should be, that's ok. When you already know you're not
happy with the way those interactions work and intend to clean them
up, what you really need is tests that give you a safety net to enable
refactoring (a green bar that you can keep green as you improve the
design). Mock-based tests aren't likely to give you that. Rather
they're probably going to mean that as you skinny down your
controllers, you'll have to change everything in at least two places
instead of just one, and meanwhile the suite won't tell you whether
things are still working.

Mock-based tests are a useful design tool, and can (in some cases) be
useful documentation of design, but they don't usually make for a good
regression suite. All three of those are important when developing
from scratch. But when taking on legacy code whose design you intend
to change, the first two goals are off the table. The third is better
served with higher-level tests.

It's definitely worth your time to read the 12-page version of Michael
Feathers' Working Effectively With Legacy Code.

I hope that wasn't too obnoxious.


More information about the mocha-developer mailing list