O'Reilly logo

Effective Testing with RSpec 3 by Myron Marston, Ian Dees

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

Connecting the Test Subject to Its Environment

When you construct your test environment, you also need to connect it to your test subject. In other words, you need to make your doubles available to the code you’re testing. There are several ways to do so, including the following:

  • Stubbing behavior on every instance of a class
  • Stubbing factory methods
  • Treating a class as a partial double
  • Using RSpec’s stubbed constants
  • Dependency injection, in its many forms

Each of these approaches has its advantages and trade-offs. As we look through them in turn, we’ll be working with the same simple example: an APIRequestTracker class that helps API developers track simple usage statistics for each endpoint. This kind of information is handy for figuring out ...

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