Mock Tips

My opinion about the best way to use mock objects changes every few months. I’ll try some mocks, they’ll work well, I’ll start using more mocks, they’ll start getting in the way, I’ll back off, and then I’ll think, “Let’s try some mocks.” This cycle has been going for years and I have no reason to think it’s going to change anytime soon.

That said, some guidelines always hold true.

The Ownership Rule

Don’t mock what you don’t own. In other words, use test doubles only to replace methods that are actually part of your application, and not part of an external framework. (Note that we violated this rule in this chapter when we stubbed ActiveRecord methods like update_attributes.)

Prescription 19Don’t mock what ...

Get Rails 4 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.