TDD: Test Doubles in Unit Testing. Should we use Fakes? Stubs? Mocks?

February 14, 2022

TDD: Test Doubles in Unit Testing. Should we use Fakes? Stubs? Mocks?

In unit testing, Test Doubles are used to replace real implementations. For example, we’d use Test Doubles to replace databases, file system, networking, etc.

Fowler wrote about Test Doubles here https://buff.ly/2q26i64, quoting below:

– Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
– Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).
– Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test.
– Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.
– Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

A useful way to capture the hierarchy (Uncle Bob https://buff.ly/2DBGy9b) “We can say that a Mock is a kind of spy, a spy is a kind of stub, and a stub is a kind of dummy. But a fake isn’t a kind of any of them. It’s a completely different kind of test double.”

The difference in usage:
– In Mockist TDD, use only Mocks (for verifying interactions)
– In Classicist TDD, typically use Fakes, Stubs, Spies(due to state verification, but may use Mocks (though not typical)

Let’s look at an example, what are some test doubles examples?
1. A Fake repository can be implemented as an in-memory list of entities.
2. A Stub repository can be implemented to return hardcoded entities, or throw exceptions.
3. A Spy email service may record the number of emails sent.

Where to find examples?

1. The quickest example is in Dave Farley’s video https://buff.ly/3hM6sse where you will see him demonstrating usage of fakes for the file system.

2. If you purchase the series “London vs. Chicago” (Uncle Bob & Sandro) https://buff.ly/365IhCV you can see examples of usage of Fakes & Stubs in Classicist TDD (e.g. fake in-memory DB implementation) and usage of Mocks in Mockist TDD.

3. In the “Software Engineering and Google”, they prefer fakes “If using a real implementation is not feasible within a test, then best option is often to use a fake in its place. A fake is preferred over other test double techniques because it behaves similarly to the real implementation”.

PERSONAL OPINION

In Classical TDD style, I prefer Fakes the most because they’re closest to the real thing, and also I get the highest amount of re-use, and can also use them in demos. If for some cases I can’t use Fakes, then I’d go for Stubs, and Spies the rarest. In Mockist TDD style, only mocks of course.

Optivem Group LLC 30 North Gould Street Sheridan, WY 82801 United States

Optivem DOO, MB 21609722, PIB 112111924, Heroja Maričića 93, Kraljevo 36000, Serbia

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.