Mocks, dummies, spies, fakes - testing your code

Testing code has different definitions and sometimes can mean different things for different people. this post is a collection for easy to come back definitions on mocks, dummies. stubs, spies and fakes.

Mocks/Test doubles

Mock is frequent used interchangeably with the term test double, mock is often used for people that didn’t read the paper definition of mock. Then, developers got used to the term mock instead of test double. Mocks, refers to the whole family of objects that are used in tests [1]. On the other hand, [2] defines this technique as substitutes or mocks objects.

Despite of the family of mocks and its definition, the goal using this technique is to test a unit of code in isolation, without touching its dependencies.

Dummies

You pass in something, and you don’t care who it is used, often the object is not used at all.

Stub

Opposed to dummies, stubs are objects created in a way that you do care how they are used. For example, to tricky an authorization to test if the user ca/can’t do certain actions in the system.

Spies

To assert that a method was called by the system under test, as the post by [1]: “You can use Spies to see inside the workings of the algorithms you are testing”.

true mocks

Is interested in the behavior, instead of return of functions. It cares about which functions were invoked, with what arguments and how often.

Fakes

Fakes have business logic, so it can drive the system under test with different sets of data.

References

  1. [1]R. C. Martin, “The Little Mocker,” 2014 [Online]. Available at: https://blog.cleancoder.com/uncle-bob/2014/05/14/TheLittleMocker.html. [Accessed: 29-Nov-2020]
  2. [2]S. Freeman, “Growing Object-Oriented Software, Guided by Tests,” 2009 [Online]. Available at: https://www.amazon.com/Growing-Object-Oriented-Software-Guided-Tests/dp/0321503627. [Accessed: 08-May-2021]