Sunday, October 30, 2011

Getting VCR to work with Rspec

I'm writing an SOAP API wrapper in Ruby, and there's no test system to use during development. Being about to use something like VCR to record the request/response from the service is great, because I can do it a controlled fashion the first time (and record it), and then do stupid things to it without getting in to trouble. It also helps my features and tests run much faster than if I had to actually ping the server every time.

I found Cucumber and VCR's official documentation good, but over-complicated for what I wanted to do. Here are the steps it actually took me to get it working quickly and simply in my features (using fakeweb).
  1. Create the file 'features/support/vcr.rb' with the following contents:
  2. require 'vcr'

    VCR.config do |c|
      c.stub_with :fakeweb
      c.cassette_library_dir = 'features/cassettes'
  3. Inside the step definitions (in my steps files under 'features/steps/'):
  4. VCR.use_cassette 'name' do
This will create the cassette file 'name' in the 'features/cassettes' directory. After recording your request/response the first time, any additional communications will be stopped by VCR, because the default options for VCR sets record to 'new episodes'.

No comments:

Post a Comment