Which test double would you use for a payment gateway: a stub, a mock or a fake?
Model answer
It depends on what the test needs, and often you use more than one across a suite. Unit tests should never call the real gateway.
Use a stub when you need a canned answer to drive the code: "the gateway approves" or "the gateway declines". It has no logic and no verification, and it is the simplest option.
Use a mock or spy when the behavior to check is the outgoing call itself, for example that the charge was sent once with the right amount and an idempotency key.
Use a fake when many tests need gateway-like behavior with state: an in-memory gateway that records charges, declines a specific test card and returns the same result for a repeated idempotency key. It costs more to write but keeps tests readable.
A dummy is an object you pass only to fill a parameter, such as a logger the test never reads.
Pick the simplest double that answers the test's question, and cover the real gateway sandbox with a few integration tests.
Key points
- A stub returns canned results such as approve or decline
- A mock or spy verifies the outgoing charge call
- A fake adds realistic state such as idempotency
- Unit tests never call the real gateway
Likely follow-ups
- How would you keep the fake gateway consistent with the real sandbox?
Common mistakes
- Using a mock when a stub would do
- Calling the real payment gateway from unit tests