O'Reilly logo

RSpec Essentials by Mani Tadayon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required