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

Get Effective Testing with RSpec 3 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.