Jon Homan, Mar 28, 2011
My pet project, lifeStreams, has a lot of interaction with external websites like Twitter and user’s blogs. Since the majority of the interesting bits of the website have to do with these external sites, I needed to find a good way to write automated tests around these external calls.
I heard of WebMock through some chatter on Twitter and came across VCR when reading WebMock’s README. WebMock is a library that allows you to fake HTTP requests to external websites and APIs. That’s basically what I needed: a way to fake an HTTP request to update a status a Twitter, for example, in my automated tests. So how did VCR come into play?
VCR records the initial HTTP response and replays that response for subsequent requests. So, once you have VCR setup, your tests will make one real request and then the recorded requests are used by WebMock. One nice option is re-recording these web requests on an interval to ensure the recorded responses don’t get stale.
Using this setup, you’re tests can speed up quite a bit. My results showed a marked improvement. This time difference was mainly due to the fact that my application makes a request to a user’s blog for their RSS/Atom feed whenever a feed is created. So, my tests were spending a lot of time making requests to figure out the feed urls. Once I got WebMock and VCR in place, I could stand running my tests again.
Soon I will have another post outlining how to get started with WebMock and VCR. In the mean time, you can view the commits to my project as I started using WebMock and VCR. The first WebMock related commit is “start with webmock”.