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