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 to avoid. All of these are defined in rspec-mocks, RSpec's test double framework.
You can think of double
as returning a dummy object. For very simple cases, we could simply use Object.new
instead. For example, in our shopping cart test, we didn't do...