The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,706 other followers

The Little Mocker (on Test Doubles; via: 8th Light)

Posted by jpluimers on 2014/08/21

Recently, I bumbped into the The Little Mocker | 8th Light by uncle Bob C. Martin.

It’s a nice conversation of a mocker, that explains about Test Doubles (with examples).

Mocks are a bit to Test Doubles like Stunt Doubles are to Body Doubles: a specific kind of Test Doubles.

The conversation step by step explains these Test Doubles probably based on Martin Fowler‘s list of Test Doubles or Mocks, Fakes, Stubs and Dummies at XUnitPatterns.com:

  • Test Dummy: returns nulls (or equivalents null) to consumers as they should not have used: the dummy is a filler in the parameter list to setup the System under Test.
  • Test Double: the generic term (sometimes informally called mocks, but they are not; see Test Mock below).
  • Test Stub: like a Test Dummy, but returns a specific value for a specific test (or group of tests). For instance return true for a LoginStub.
  • Test Spy: like a Test Dummy (or evenTest Stub) but also makes a note (or log) of each call (so you can spy on the calls). It can lead to coupling, so might make your tests fragile.
  • Test Mock: like a Test Spy, but allows verification of certain existence of absence notes or log entries (called Expectations). Like ensuring a certain call to theTest Mock was made with certain parameters.
  • Test Fake: unlike a Test Stub, as fakes have business-like behaviour (so it simulates business behaviour to a certain extent, for instance by having certain data-sets in memory, or responding with certain business logic for instance returning true for certain Login conditions). It also means thatTest Fakes need unit tests of their own.

The links in the above list all contain a small diagram showing the dataflow between the Test Double, System under Test, stc. Jeff Atwood has some nice graphics to go with them at Test Doubles: A Taxonomy of Pretend Objects.

In practice, Stubs and Spies are probably used most, followed by Mocks (either in code or with a Mocking tool).

Notes:

  1. The above order is how the story explains them, not the list by Martin Fowler or the list on WikiPedia.
  2. Want to know more on how to use Test Doubles? Read Test Double at XUnitPatterns.com and Mocks Aren’t Stubs or buy xUnit Test Patterns: Refactoring Test Code: Gerard Meszaros: 9780131495050: Amazon.com: Books.

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: