docs/concepts/mocks/index.md
::: warning Consider Using Fakes Instead Mocks are powerful but easy to overuse. Consider using fakes with explicit assertions instead. Fakes provide simpler behavior replacement without coupling tests to implementation details. Reserve mocks for cases where upfront expectations truly clarify test intent. :::
Mocks (and expectations) are fake methods (like spies) with pre-programmed behavior (like stubs) as well as pre-programmed expectations.
A mock will fail your test if it is not used as expected.
Mocks should only be used for the method under test. In every unit test, there should be one unit under test.
If you want to control how your unit is being used and like stating expectations upfront (as opposed to asserting after the fact), use a mock.
Mocks come with built-in expectations that may fail your test.
Thus, they enforce implementation details. The rule of thumb is: if you wouldn't add an assertion for some specific call, don't mock it. Use a stub instead.
In general you should have no more than one mock (possibly with several expectations) in a single test.
Expectations implement both the spies and stubs APIs.
Use mocks when:
Use stubs when:
onCall(), onFirstCall())withArgs())Use fakes when: