Skip to content
BENIBIX Learn

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.

Development🧪 Testing & Quality

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.

Example
Jack's tests use a fake user store that keeps users in a list. 500 tests run in 2 seconds, with no database to start.
Why it matters
Fakes let lots of tests run fast. They still act like the real part, not just give one set answer.
Full entry
Development🧪 Testing & Quality

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.

Example
Lucy's app shows an umbrella when it rains. Her test uses a stub weather service that always returns rain, so the umbrella must show.
Why it matters
Real services change their answers. A stub lets you test each case on purpose, including the rare ones.
Full entry

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.