Using Partial Doubles Effectively

Partial doubles are really easy to use: just stub or expect a message on any object! However, we said in Partial Doubles that we consider their usage to be a code smell. We’d like to flesh that statement out a bit now.

Most unit tests involve a mixture of two types of objects:

  • Real objects: typically the subject of the example
  • Fake objects: collaborating test doubles used to construct an environment for the test subject

Partial doubles don’t fit neatly in this hierarchy. They are partially real and partially fake. Are they part of what you are testing or part of the environment you are constructing? When an object’s roles are unclear, your tests can be harder to reason about.

We prefer not to mix these roles ...

Get Effective Testing with RSpec 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.