VCR versus Webmock

April 12, 2016

When we first learned how to test external APIs in Rails, we used VCR cassettes. To create a cassette, all we needed to do in our test was wrap the API call with VCR.use_cassette. It seemed simple enough, I used them when consuming the Instagram API. It was nice to have a snapshot of the response so easily made and ready to go. One of our instructors warned us to clear our cassettes occasionally though, because our tests would pass even when the endpoint changed its response, causing problems in our applications. I didn't really have any issues with VCRs until I started working on my personal project, an air quality alerting app, that used a somewhat temperamental API from Breezometer. Occasionally, my tests would fail and I would get the dreaded error about VCR not sure how to handle the HTTP request. Surely there must be another way to test a call to an external API?

This week at Turing we began a project where we get to work on an application already in production called Looking For Me, a site for Turing grads to find development related jobs. It's exciting because we get a taste of what it's like to work on a development team, and inherit code that was written mostly by Turing alumni. The app pulls in jobs already from two external services, and they are tested using Webmock and Sinatra. This took a little getting used to since I had always used VCR, but I can definitely now see the advantages.

There's more setup involved at first of course. You use Webmock to route requests from tests, and Sintra and fixtures to handle the response. Below, the FakeSuperCool class is the Sinatra application that provides the fake response to the stubbed request to any url that contains supercool.com.

Here is an example of the FakeSuperCool Sinatra application:

Depending on certain parameters, a different fixture will be called for the response needed.

You never need to hit the external service ever (like you do the first time you record a VCR cassette). It's also easier to reproduce errors for testing. This blog post from QuickLeft also describes the advantages of depending less on VCR and using Webmock, a faster running test suite! You will still need to keep an eye on the external API you're using for any changes, and update your tests. Check out this article from thoughtbot for more information on how to setup Webmock and Sinatra.

After getting used to Webmock, I still think that VCR is pretty useful. When solely relying on Webmock for this week's project, I made a slight error in the url when making a get request with Faraday that I didn't catch until I ran a rake task in development and it failed when trying to make the API call. (If you are wondering what the error was, I had put in & instead of a ? - I know, I know, silly). If I had used VCR for my first test, this error would have been caught immediately.

...back to posts