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

Advanced state control with hooks and mocks

Let's delve deeper into hooks and mocks. There are at least two related features that would be nice to have in WeatherQuery:

  • Store and retrieve a history of weather queries
  • Keep a count of the total number of API requests sent so we can throttle usage to avoid flooding the API with too many requests

As you can see, both of these features require us to set up a certain state before we can test them. In this case, we want to avoid mocking the actual state changes, because we would then have little or nothing to test. We can mock HTTP requests and other details not related to the state change, but we want to actually perform multiple queries and then make an assertion, rather than mock a state when multiple ...

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