Search code examples
ruby-on-railsruby-on-rails-4rspecfaradayvcr

VCR does not record cassettes using faraday


I have scoured the internet for an answer to this, including most of the other "suggested" questions on SO.

I have configured VCR in rspec, but when running the tests VCR never records a tape (tape dir remains empty) and runs the HTTP request every time.

My VCR config looks like this:

VCR.configure do |c|
  c.cassette_library_dir = Rails.root.join('spec', 'vcr_cassettes')
  c.hook_into :faraday
  c.default_cassette_options = { record: :new_episodes }
  c.configure_rspec_metadata!
  c.allow_http_connections_when_no_cassette = true
  c.ignore_hosts 'api.linkedin.com'
  c.debug_logger = $stderr
end

I am using faraday because the LinkedIn gem I am using (https://github.com/bobbrez/linkedin2) is using faraday to make requests already. The test spec I'm running looks like this:

describe API::V1::Connections, type: :request do
  subject { LinkedIn::Client.new scope: %i(r_network) }

  describe 'GET /api/v1/connections/:username', vcr: { cassette_name: 'profile' } do
    it 'fetches the user profile' do
      profile = subject.profile
      expect(profile).not_to be_nil
    end
  end
end

It passes each time, but only after making the API request every time. The VCR debug log shows the exact same thing on every run of the spec:

[Cassette: 'profile'] Initialized with options: {:record=>:new_episodes, :match_requests_on=>[:method, :uri, :headers], :allow_unused_http_interactions=>true, :serialize_with=>:yaml, :persist_with=>:file_system}
[faraday] Handling request: [get https://api.linkedin.com/v1/people/~:(connections)?oauth2_access_token=xxxx&format=json {"User-Agent"=>["Faraday v0.9.0"]}] (disabled: false)
[faraday] Identified request type (ignored) for [get https://api.linkedin.com/v1/people/~:(connections)?oauth2_access_token=xxxx&format=json {"User-Agent"=>["Faraday v0.9.0"]}]

It seems like it should record a cassette, but no .yml files ever show up in the spec/vcr_cassettes dir

(Using Rails 4, rspec 3, vcr 2.9)


Solution

  • You've configured VCR to ignore requests made to 'api.linkedin.com' with this line:

    c.ignore_hosts 'api.linkedin.com'
    

    ...which prevents it from recording those requests.