Thursday, April 18, 2013

How to use VCR, Webmock with RSpec

1. Add the gems to the Gemfile under test group:
gem "webmock"
gem "vcr"

2. bundle

3. require 'vcr' as the first line in spec_helper.rb

4. Add

VCR.configure do |c|
  c.cassette_library_dir = 'spec/fixtures/cassettes'
  c.hook_into :webmock
  c.ignore_localhost = false
  c.allow_http_connections_when_no_cassette = true

inside the configure block.

5. Add WebMock.allow_net_connect! in the before block in the specs.
6. Wrap your network calls using :
       VCR.use_cassette do
              controller code that accesses network goes here