Mocks, stubs, and doubles
The word
mock is generic. Often the words stub, fake, and double are used to mean the same thing. To be precise, we could make a distinction between mocks and stubs. A mock is a fake object that stands in the place of an actual object. A stub is a fake method that is executed in place of an actual method. In practice, the terms are used interchangeably. In fact, our Object#mock
method should have been named Object#stub
if we were being consistent.
RSpec uses the method double
to handle mocking. This is aliased as stub
and mock
, which can be confusing, but these aliases are both deprecated, so you shouldn't use them. There is also a method called spy
, which is a convenience method based on double
that I encourage you ...
Get RSpec Essentials 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.