Chapter 3. Taking Control of State with Doubles and Hooks

In this chapter, we'll learn how RSpec implements the general testing concepts of stubs, mocks, and spies with double. We'll start by implementing our own custom mocking method and use it to show off some fun tricks. This will help us understand how mocking works. We'll also appreciate the variety of mocking tools that RSpec offers after we implement one of our own. Then we'll learn how to use RSpec hooks to set up and tear down state related to our tests. Here is what we will cover in this chapter:

  • The role of stubs, mocks, and spies in testing
  • How to use RSpec's double
  • Spying on methods and objects with expect and to_receive
  • Setup and teardown with before and after hooks

Why mock?

A unit in ...

Get RSpec Essentials now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.