Mocks, dummies, spies, fakes - testing your code

Last updated Nov 5, 2023 Published Nov 29, 2020

The content here is under the Attribution 4.0 International (CC BY 4.0) license

Testing code has different definitions and sometimes can mean different things to different people, however, the hidden convention that sticks until today is “mocking”, regardless of the type of test double. this post is a collection of easy-to-come-back definitions of mocks, and dummies. stubs, spies and fakes.

Mocks/Test doubles

Mock is frequently used interchangeably with the term test double, mock is often used for people who didn’t read the paper definition of mock. Then, developers got used to the term mock instead of test double. Mocks refer 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. The following picture from [3] depicts visually the different types of test doubles:

[Venn diagram of Test Doubles from Agile Technical Practices Distilled]

Despite the family of mocks and its definition, the goal of 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 don’t 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 they 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]
  3. [3]P. M. Santos, M. Consolaro, and A. Di Gioia, Agile Technical Practices Distilled: Become Agile and Efficient by Mastering Software Design. Packt Publishing, Limited, 2019 [Online]. Available at: https://books.google.es/books?id=sBT-xgEACAAJ