Comparison
MockvsTest double
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 →Test double
you swap the real collaborator for a stand-in, because the real one is slow, remote, or has side effects you cannot have in a test.
The umbrella term for any object substituted for a real dependency in a test: stubs, mocks, fakes and spies are all test doubles. Naming the specific kind matters because they answer different questions — some make a test possible, others make an assertion. Calling everything a mock is why test reviews go in circles.
Full entry →