When you feel TDD is difficult when you first started TDD, that might sign that there are improvements to make in the design.
There are 5 common anti-patterns.
- The Liar
Passes all tests with no useful assertions, which could be caused by chasing test coverage.
=> Write the test first, which fails with a purpose.
- Excessive Setup
Test and Code are highly coupled. Hard to maintain.
=> Improve Abstraction and Separation of Concerns. Write Test first.
- The Giant
Many lines of code and many assertions which caused by writing the code first before writing testing. This isn’t easy to understand.
=> Decomposed into multiple test cases.
- The Mockery
Use so many mocks, which is similar to the excessive setup.
=> Review design of code. The same solution as #2.
- The inspector
Violate encapsulation to make assertions, which can be caused by poor dependency injection.
=> Never compromise encapsulation for testing. Design for testability.
In summary, to practice TDD correctly, write a test first, which will help design the code testable.