Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec + savon + vcr not recording

I can't record anything with vcr, I have such setup:

spec_helper.rb

require 'vcr'
VCR.configure do |c|
  c.cassette_library_dir = 'spec/cassettes'
  c.hook_into :webmock
  c.configure_rspec_metadata!
  c.default_cassette_options = { :record => :new_episodes }
end

And test:

describe SomeClass do
  describe '#foo', vcr: true do
    it 'should do http request', do
      expect(subject.do_request).to be_true
    end
  end
end

Running that spec results in:

.HTTPI executes HTTP GET using the net_http adapter
SOAP request: (...)

Exactly like without vcr. Unfortunately there's nothing in documentation. I found similar issue reported here, but requiring httpi doesn't give any effect. What should be done to get this working?

like image 279
zrl3dx Avatar asked Dec 18 '13 20:12

zrl3dx


1 Answers

For me to get VCR to record SOAP requests that correctly match took me specifying more matchers for it to use. Since with SOAP the url endpoint is usually the same but the body content/headers is different per request. Notice, :method, :uri, :headers. You can also have it match on body but this was enough for my use case since our headers are pretty detailed per request. I'm using this:

VCR.configure do |c|
  c.hook_into :webmock # fakeweb fails with savon
  c.ignore_hosts 'www.some-url.com'
  c.configure_rspec_metadata!
  c.ignore_localhost                        = true
  c.cassette_library_dir                    = 'spec/support/vcr_cassettes'
  c.allow_http_connections_when_no_cassette = true
  c.default_cassette_options                = { allow_playback_repeats: true, match_requests_on: [:method, :uri, :headers] }
  # c.debug_logger                            = File.open(Rails.root.join('log/vcr.log'), 'w')
end

Then I tag the spec with an empty hash in case I want to override the global VCR config, like:

describe SomeClass do
  describe '#foo', vcr: {} do
    # test something
  end
end

And lastly the more requests you can have VCR/Webmock ignore the easier to debug. So if you have lots of JS calls or similiar add those requests to the 'ignore_hosts' option. My last line in the global config shows how to have VCR log what it's doing, also helpful.

like image 135
SupaIrish Avatar answered Sep 28 '22 08:09

SupaIrish