When to Use Each Type of Test Double
You'll learn how to choose the right test double for each testing scenario based on what you need to verify and how tightly you want to couple your tests.
Matching Test Doubles to Your Testing Needs
Each type of test double serves a different purpose. Choosing the right one depends on three key questions: What do I need to verify?, How complex is the interaction?, and How much do I want my test to know about implementation details?
Decision Guide by Test Double Type
Dummies are perfect when you need to satisfy a function signature but the parameter isn't actually used in your test scenario. Use them when you're testing something completely unrelated to that dependency.
Stubs shine when you need to control indirect inputs—the data coming from a dependency. Use them when your test focuses on how your code responds to certain conditions, not on whether it called something correctly.
Fakes work best for complex dependencies like databases or file systems that would slow down tests. Use them when you need realistic behavior but can't afford the real thing's cost or complexity.
Spies are ideal when you care about both the return value and whether/how something was called. Use them for light verification without fully committing to interaction testing.
Mocks are your choice when the interaction itself is what you're testing—when calling the right method with the right parameters is the behavior. Use them sparingly, as they create the tightest coupling to implementation details.
The Coupling Trade-off
The more your test double verifies interactions (moving from dummies → stubs → spies → mocks), the more your test knows about how your code works internally. This makes tests more brittle but can catch important behavioral issues.
Key Takeaway: Choose dummies and stubs for loose coupling and testing outcomes; choose spies and mocks when the interaction itself matters; use fakes for complex dependencies that need realistic behavior.