Skip to content

What it is

A fake is a test double with a working but simplified implementation: it behaves like the real collaborator, but with shortcuts that make it unsuitable for production and ideal for tests.

The canonical example is an in-memory repository that replaces a real SQL database. The fake implements the same interface — save, find, delete — but stores records in a dictionary. The test runs the same code path as production; the database is replaced by a Map.

class InMemoryUserRepository implements UserRepository {
    private array $users = [];
    public function save(User $u): void { $this->users[$u->id] = $u; }
    public function find(string $id): ?User { return $this->users[$id] ?? null; }
}

The distinction from the other doubles is implementation, not behaviour. A stub returns a canned answer; a mock asserts an expectation; a spy records calls; a fake works, just less completely than the real thing.