Joël Quenneville presents the why, what, and how of testing in isolation!
Why Do We Test?
Test the What, Not the How
- When testing, it's important to focus on what an object does, not how it does it
- Private methods are implementation details of an objects public behavior
- Weekly Iteration: Test Doubles
- Part of the reason it was so easy to isolate the
Formatter was because the collaborator was passed in as an argument
- Dependency injection is a best practice used to keep objects loosely coupled to their collaborators
- There's a virtuous cycle here: Loosely coupled objects are easier to test in isolation and writing unit tests pushes you to couple your objects loosely
- Weekly Iteration: Dependency Management in Rails
Law of Demeter
- Demeter says not to play the telephone game where you tell your collaborator to tell their collaborator to tell their collaborator to do something
- Demeter violations result in high coupling. Not only are we coupled to each of the objects in the chain, but also to the structure of the chain. If any of the relationships change, our Formatter will break.
- Weekly Iteration: Law of Demeter
- Don't stub the subject that you are testing
- In the worse case, you've written a test that's a Tautology, a self-referential statement that's guaranteed to always be true
Principles of Testing
- Isolate your subject
- Don't mess with the subject
- If you feel pain, maybe rethink your implementation