External Dependencies and Infrastructure
You'll learn how Test-Driven Development becomes challenging when your code needs to interact with real databases, file systems, networks, or external services—and why these dependencies deserve special treatment.
The Problem with Real Infrastructure
When writing tests, you want them fast, reliable, and repeatable. But real external dependencies break all three rules:
- Databases need setup, are slow to query, and can leave behind dirty state
- File systems vary between machines and CI environments
- Networks are unreliable and unpredictable
- Third-party APIs may have rate limits, cost money per call, or return different data over time
Imagine trying to test a banking application that calls a real payment processor. Every test run would charge real money, depend on their servers being up, and fail randomly when the network hiccups. That's not sustainable.
The TDD Approach
This is where test doubles (which you've already learned) become essential. You create boundaries in your code—interfaces or abstractions—that separate your business logic from infrastructure concerns.
Example boundary: Instead of calling a database directly, your code calls an OrderRepository interface. In production, that's implemented by a real database class. In tests, you use a fake, stub, or mock.
The key insight: test the behavior of your code, not the infrastructure. Your unit tests verify logic using test doubles. You write a small number of integration tests to verify the real infrastructure works, but keep those separate from your fast TDD feedback loop.
When You Need the Real Thing
Some tests must use real infrastructure—testing database queries, file parsing, or API integrations. Keep these slower tests in a separate suite and run them less frequently.
Key Takeaway: External dependencies break TDD's fast feedback loop. Design clear boundaries, use test doubles for unit tests, and isolate real infrastructure tests into a separate, slower suite.