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 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.