Comparison
MockvsStub
Mock
you assert that the email service was called exactly once with this address, and the test fails if it was not.
A double with pre-programmed expectations about how it will be called, which fails the test when they are not met. It verifies interactions rather than results, which is right when the observable effect is the call itself — an email sent, a message published. Overusing mocks welds the test to the implementation, so refactoring breaks tests without breaking behaviour.
Full entry →Stub
you hard-code the collaborator to return a fixed value so the code under test has something to work with.
A double that returns canned responses, providing inputs to the code under test. It has no assertions and no logic; it exists so the test can reach the code path you care about. If your test asserts on what the stub was called with, you have written a mock.
Full entry →