Mock vs fake
A mock is a test double pre-programmed with expectations about the calls it should receive, and it fails the test when those calls do not arrive as specified. A fake is a working but simplified implementation of the real component, such as an in-memory database. A mock checks the conversation, while a fake only takes part in it.
The longer version
Both are test doubles, stand-ins for a component that the test does not want to use for real, and the vocabulary comes from Gerard Meszaros. Martin Fowler summarised it in 2006. Fakes, he wrote, "actually have working implementations, but usually take some shortcut which makes them not suitable for production (an InMemoryTestDatabase is a good example)". Mocks "are pre-programmed with expectations which form a specification of the calls they are expected to receive". Meszaros's own pattern pages on the xUnit Patterns site give each a purpose in one line. A mock object is "a test-specific object that verifies it is being used correctly by the SUT", the system under test, and a fake object replaces a component that the system depends on "with a much lighter-weight implementation". The fake behaves, and the mock judges.
Take code that archives an uploaded document: it writes the file to a storage service and records the path. With a mock, the test states in advance that the storage double must receive one write, to a path ending in the document's identifier, and the double fails the test if the write is missing, repeated or addressed elsewhere. Nothing is stored anywhere. With a fake, the test installs an in-memory store that keeps files in a dictionary, runs the archiving code, then asks the fake for the file at the expected path and compares the contents. No expectation about calls was written. The code may write once or in three chunks, and the test passes as long as the right file can be read back. The mock test verifies behaviour, meaning the calls that were made. The fake test verifies state, meaning what is there afterwards.
The costs fall differently. A mock is cheap to create and belongs to one test, and it ties that test to the present shape of the code, so a harmless change in how the calls are made breaks it. The xUnit Patterns site notes that a fake object helps avoid that kind of over-specified test, since the exact calling sequence is not written into the test. A fake costs more at the start, being a small program that has to be written, kept in step with the real component and trusted. A fake that accepts a file name the real service would reject lets tests pass against behaviour that does not exist, and running one set of tests against both the fake and the real component is one guard. A stub is the third relative and the simplest: it returns canned answers and keeps no state, while a fake returns what earlier calls put into it. Where the interaction is itself the requirement, such as an audit entry written exactly once, a mock remains the direct tool.
Side by side: a mock verifies interactions, and a fake supports verifying state. A mock is usually generated by a mocking library for one test, and a fake is hand-written code shared by many tests. A mock knows nothing of the domain beyond the calls it expects, and a fake implements a simplified version of the real behaviour. A mock breaks when the calls change, and a fake misleads when the real component changes and the fake is not updated. When to use which: use a mock when the call is the outcome that matters, such as a message sent, a payment requested or an audit entry written, and there is no state to inspect afterwards. Use a fake when the code talks to a dependency many times and many tests need it to behave plausibly, as with a repository, a file store or a clock. Microsoft's Engineering Fundamentals Playbook leans the same way, saying that starting without a mocking framework and writing fake implementations "will lead to a more healthy code base". Where the real component is fast and safe to use in a test, neither double is needed.
Common questions
Mock vs fake: what is the difference?
A mock is set up with expectations about the calls it should receive and fails the test when the calls differ. A fake is a working, simplified implementation of the real component, such as an in-memory database, which the test uses as it would use the real one. A test with a mock checks which calls were made, and a test with a fake checks the resulting state.
Is an in-memory database a mock or a fake?
An in-memory database is a fake. It stores what the code writes and returns it when asked, so it has working behaviour of its own, with the shortcut that nothing reaches disk. Martin Fowler's summary of test doubles gives an in-memory test database as his example of a fake. A mock, by contrast, stores nothing and only compares the calls it receives with the calls it was told to expect.
When should a test use a fake instead of a mock?
A fake fits when many tests need the same dependency to behave realistically across several calls, for example saving a record and reading it back. The xUnit Patterns site recommends a fake object when tests need more complex sequences of behaviour than is worth implementing in a stub or a mock. A mock fits when the call itself is the behaviour being verified.
How is a fake different from a stub?
A stub returns fixed answers that the test chose in advance and remembers nothing between calls. A fake has working logic and state: what one call stores, a later call returns. Gerard Meszaros draws the line by control. A stub is a point through which the test injects inputs into the code under test, and a fake only lets the interactions happen in a self-consistent way.
What are the risks of using a fake?
A fake is a second implementation, and it can drift from the first. If the real component rejects an input, enforces a limit or fails in a way the fake does not copy, tests pass against behaviour the product will never meet. A fake also needs its own maintenance. Running one shared set of tests against the fake and the real component keeps the two in step.
What are the five kinds of test double?
Gerard Meszaros named five, and Martin Fowler's article Mocks Aren't Stubs summarises them. A dummy is passed around but never used. A fake has a working implementation that takes a shortcut. A stub provides canned answers to calls made during the test. A spy is a stub that also records how it was called. A mock is pre-programmed with expectations about the calls it should receive.
Why do people call every test double a mock?
Mocking libraries generate all kinds of double, stubs and spies included, and their name has become the everyday word for the whole family. The distinction still matters when choosing how a test verifies its result: with a mock it checks the calls that were made, and with a fake it checks the state afterwards. Saying which one is meant avoids the confusion the loose usage causes.
Bug reports that arrive ready to triage.
Collect replay, console, network and browser context with the Free plan.