Frequent questions about mocks: when to prefer them over stubs, what "behaviour verification" means, and why they are controversial.
- Mock or stub?
Use a stub when you only need a dependency to answer. Use a mock when the test's purpose is to assert that a specific collaboration happened.
- What is behaviour verification?
Checking the calls made during the test, instead of the state left after it.
- Why are mocks controversial?
Because they couple the test to the implementation. A refactor that preserves behaviour can break every mock in the suite.
- Can a mock replace an integration test?
No. A mock proves the unit asked for the right thing; only an integration test proves the real system does the right thing.