My very simplified answer is:
- mocks are objects that have a similar interface as something else
- stubs are fake methods and return a specific answer
With both we are trying to achieve the same thing: we want to test a specific unit (model/view/controller/module) in isolation. E.g. when we are testing the controller, we do not want to test our model, so we use a mock. We want to make sure the correct methods are called, e.g. find
. So on our mock, we have a stub that will return something predefined, without actually going to the database.
So we test for expectations: the methods that we expect to be called (on other units), without actually calling them. The test of that specific method, should have been covered in its own test.