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 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. The following picture from [3] depicts in a visual way the different types of test doubles:

Venn diagram of Test Doubles from Agile Technical Practices Distilled

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]
  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