Fake vs Stub
People think a fake is the same as a stub, and it is not. A stub hands back a fixed answer, whatever you ask it. A fake really works, just in a small way, such as a pretend bank that keeps balances in memory. Put money in a fake and you can take it out again. A stub cannot do that.
Fake (Testing)
A small copy of a real part. It works, but only in tests.
A fake is a test double that really works, but in a simple, cheap way. A common one is an in-memory database that keeps data in a list instead of on disk. It behaves like the real thing for the cases your tests need. It is much faster and needs no set-up. Fakes are not fit for real use, because they skip things like safety and saving data for good.
Stub (Testing)
A fake part that always gives the answer you chose, so tests stay the same.
A stub is a test double that returns a fixed answer you chose ahead of time. It does not check how it was called; it just answers. You use it to put your code in a known state, like a weather service that always says rain. That makes a test give the same result every time. Where a mock checks the call, a stub only feeds the answer.
In one line
- Fake (Testing)
- A small copy of a real part. It works, but only in tests.
- Stub (Testing)
- A fake part that always gives the answer you chose, so tests stay the same.