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. Use test doubles only to replace methods that are part of your application, and not part of an external framework. (Note that you violated this rule in this chapter when you stubbed ActiveRecord methods like update_attributes.)

Prescription 22Don’t mock what you don’t own.

One reason to mock only methods ...

Get Rails 5 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.