A good, simple overview of the basic approaches to testing external services in Rails:
tl;dr:
- Use webmock (catch request (with matchers), and make a response explicit)
- Use VCR (record responses, and play back)
- Use dependency injection: you typically insert a live dependency for the API, but in tests you can use a Fake dependency to handle stuff. However, your actual service class won’t be tested anymore
-
Set up your external services to allow for adapters from the start, a variant of dependency injection
- Use a fake (local) service: tough to set up, but works even for testing with other non-rails integration suites
I recently had fun of using a hybrid webmock / fake service approach. A blog is plannedA blog has been written.