Testing for Error Cases

The application design allows multiple ways to simulate errors for testing purposes:

  • You can make an API call to the actual service that results in an error and then capture the result using VCR. You can use this approach in an integration or adapter test.

  • You can stub a method that’s internal to the adapter. For example, you can stub the client method to return a double that simulates an API error. You’d use this in an adapter test.

  • You can stub the adapter to return an unexpected value in a client test.

Which approach you choose depends on the details of the library you’re working with. Often, stubbing the external service makes sense for the same reason that stubbing ActiveRecord methods does—crafting a call that ...

Get Rails 5 Test Prescriptions 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.