TDD and Single Responsibility
What you'll learn: How complicated test setup reveals that your code is doing too much.
The Test Setup Signal
When you practice TDD, your tests act as an early warning system for design problems. One of the clearest signals? If your test setup feels painful, your code probably has too many responsibilities.
Test-Driven Development naturally pushes you toward the Single Responsibility Principle (SRP) — the idea that a class or function should have one clear reason to change. Here's how: when you write the test first, you immediately feel the pain of complex dependencies and tangled concerns.
What Test Pain Looks Like
If you find yourself needing to:
- Create many test doubles (stubs, mocks, fakes) just to instantiate your class
- Set up elaborate object graphs before running one simple test
- Mock multiple different collaborators in every test
- Write long, confusing Arrange sections
...your code is screaming "I'm doing too much!"
The TDD Feedback Loop
A class with a single, well-defined responsibility requires minimal test setup. You arrange a simple input, act on the class, and assert the result. Clean and fast.
But a class juggling multiple concerns forces you to configure database connections, external services, formatting logic, and business rules all at once. The test becomes a mirror of the design problem.
Key Takeaway: In TDD, complex test setup isn't just annoying — it's valuable feedback that your class violates the Single Responsibility Principle. Listen to your tests: they're telling you to split responsibilities into smaller, focused components.