21 December 2024

Mocks, fakes, and stubs

Mocks, fakes, and stubs definitions

  • Classification between mocks, fakes, and stubs is highly inconsistent across the literature.
    • Consistent among the literature, though, is that they all represent a production object in a testing environment by exposing the same interface.
    • Which out of mock, fake, or stub is the simplest is inconsistent, but the simplest always returns pre-arranged responses (as in a method stub).
    • On the other side of the spectrum, the most complex object will fully simulate a production object with complete logic, exceptions, etc.
    • Whether or not any of the mock, fake, or stub trio fits such a definition is, again, inconsistent across the literature.
      • For example, a mock, fake, or stub method implementation between the two ends of the complexity spectrum might contain assertions to examine the context of each call.
      • For example, a mock object might assert the order in which its methods are called, or assert consistency of data across method calls.
  • In the book “The Art of Unit Testing” mocks are described as a fake object that helps decide whether a test failed or passed by verifying whether an interaction with an object occurred.
  • Everything else is defined as a stub.
  • Fakes are anything that is not real, which, based on their usage, can be either stubs or mocks.

Mock Object practice

  • In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways, most often as part of a software testing initiative.
    • A programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behavior of a human in vehicle impacts.
    • The technique is also applicable in generic programming.
    • Mock objects are used to simulate the behavior of a given object in order to cope with dependencies and isolate the system under test for controlled testing.
  • For more information, it is possible to use the Test Driven Development (TDD) approach without mock objects.
  • A mock object is a type of Test Doubles.

Test Double

  • There are at least five types of Test Doubles with some differences :
  • Test Stub,
  • Mock Object,
  • Test Spy,
  • Fake Object,
  • Dummy Object

More informations for Scrum Testing

Scrum Testing and Practices

More informations for the Scrum PSD certification here.

Updated : 01/10/2021

Leave a Reply

Your email address will not be published. Required fields are marked *