The Isolated Testing Challenge
Yesterday at the Laracon Online conference, I gave a presentation where I argued that collaborator isolation in tests handcuffs you to a specific implementation and cripples your ability to refactor.
Since refactoring is a core activity in Test-Driven Development, I claimed that isolated unit testing is therefore incompatible with TDD (
It's easy to find claims (whether true or not) that many seasoned professionals believe it's important to always test your code in complete isolation from all collaborators, and that anyone who follows the "mockist" school of TDD would never use a real collaborator in a test.
My conversations with experienced "mockist" testers have taught me otherwise (they use real collaborators in their tests regularly), but nevertheless these ideas continue to be parroted across Twitter, Reddit, and other online discussion forums.
The accompanying attitude is usually something like this:
"If testing something in isolation prevents you from refactoring, you're either doing it wrong, or your tests are trying to tell you that your code is poorly designed."
Below are three implementations of the same class that would all produce the exact same outcome as observed from the outside.
Write a unit test for this class that tests it in isolation from its collaborators, and passes with all three of these implementations.