A stub is an empty function which is used to avoid unhandled exceptions during tests:
function foo(){}
A mock is an artificial function which is used to avoid OS, environment or hardware dependencies during tests:
function foo(bar){ window = this; return window.toString(bar); }
In terms of assertions and state:
- Mocks are asserted before an event or state change
- Stubs are not asserted, they provide state before an event to avoid executing code from unrelated units
- Spies are setup like stubs, then asserted after an event or state change
- Fakes are not asserted, they run after an event with hardcoded dependencies to avoid state
References