stubs vs mocks vs fakes

Last Updated: 2022-02-04
  • stubs: no logic, only returns what you tell it to return. Can be used when you need an object to return specific values in order to get your code under test into a certain state.
  • mocks: with expectations about they way they should be called, and a test should fail if it is not called that way. Mocks are used to test interactions between objects.
  • fakes: do not use a mocking framework. They are lightweight implementations of the APIs, but not suitable for production (e.g. an in-memory databases). Fakes can be used when you cannot use a real implementation in your test, e.g. if the real implementation is too slow or it talks over the network.