Types of Test Doubles

When we first introduced test doubles, we hinted that there are different names for doubles—mocks, spies, and so on. But we glossed over some of the differences. Let’s take a closer look now.

There are a couple of different ways to think about a test double. One is the usage mode of the double—that is, what you’re using it for and what you’re expecting it to do. The other thing to consider is how the double is created. We’ll call this the double’s origin.

Here are the usage modes we’ll be talking about in this chapter:

Stub

Returns canned responses, avoiding any meaningful computation or I/O

Mock

Expects specific messages; will raise an error if it doesn’t receive them by the end of the example

Null Object

A benign test ...

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.