Toxic Elephant

Don't bury it in your back yard!

I'm Not a Mockist

Posted by matijs 04/11/2007 at 13h56

Let’s talk about mocking.

It apparently is the hot new thing in Rails land. And like earlier silver bullets, it’s becoming a religion: Acolytes would rather write ten lines to set up a mock object than one to instantiate an actual one.

Honestly, I have tried to understand the benefits of mocking, but I just don’t see them1. Then I thought I would have to write a long article about how mocking is flawed, and would you people please all see sense already. Luckily, Martin Fowler did it for me, and with a lot more objectivity. Go read his Mocks Aren’t Stubs. In addition to explaining the difference between Mocks and Stubs (a difference often overlooked by the religious), he explains why you might not want to use what he calls “mockist testing”.

So, in the spirit of religious tolerance, I can now say: I’m not a Mockist.

My particular reasons?

  • Mockist testing is not DRY: Each class’ behavior is now defined in its code, its unit tests, and each time it is mocked.
  • Mockist testing tests a particular implementation of behavior, making refactoring harder.
  • Mockist testing makes writing your tests more work, inviting you not to test.

Next time you find yourself complaining that setting up your mock objects is such a lot of work, ask yourself: “Am I a Mockist?”. Maybe you’re not.

1 Well, one minor benifit I can see that you can start writing and testing your views before having written your models. I never seem to want to do that anyway, though.

Tags no comments no trackbacks

Comments

Comments are disabled