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 19 | Donâ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.