Isolation Is the Goal
Unit tests are supposed to be fast, deterministic, and isolated. They should test your logic, not the availability of an external API or the state of a shared database.
If your processPayment() function calls the Stripe API, a unit test that actually hits Stripe has several problems:
- It is slow (network round-trip adds hundreds of milliseconds per test).
- It is non-deterministic (Stripe might be down, rate-limit you, or return different responses).
- It has side effects (it charges a real card or consumes test quota).
- It requires credentials to be present in the test environment.
To test this logic without touching the real outside world, we use Test Doubles — fake versions of real dependencies. The two most important types are Stubs and Mocks. These terms are often used interchangeably in casual conversation, but they mean different things and the distinction matters.