ruby-on-railsapirspecvcr

Playing a cassete whose request is not inside the use_cassete scope using VCR for Rails


I've built an API (MyAPI) that connects to an external API for authentication (ExAPI).

For all the requests to MyAPI where the user needs to be authenticated, he sends a cookie with a token that is then sent to ExAPI, and then it gets the user information.

I've recorded a cassete with this request:

describe 'VCR-RSpec integration' do
  def make_http_request
    connect = Faraday.new(url: (ENV['EX_API']).to_s) do |faraday|
      faraday.request :url_encoded
      faraday.response :logger 
      faraday.adapter Faraday.default_adapter 
    end
    connect.authorization :Bearer, ENV['USER_TOKEN']
    connect.get('/auth/...')
  end

  skip 'without an explicit cassette name' do
    it 'records an http request' do
      VCR.use_cassette('user_token') do
        expect(make_http_request).to be_success
      end
    end
  end
end

So, in my code, if I do a call to user_stories it expects a cookie with the user_token sends it to the ExAPI and if valid, executes the desired action.

This is how the spec is:

describe UserStoriesController, type: :controller do
  before(:each) do
    set_cookies
  end

  context do
    let!(:user) {
      FactoryGirl.create(:user)
    }
    let!(:some_user_stories) {
      FactoryGirl.create_list(:user_story, 3, user: user)
    }

    describe 'GET index' do
      it 'returns a successful 200 response' do
        VCR.use_cassette('user_token') do
          get :index
          expect(response).to be_success
        end
      end
    end
  end
end

The problem is that the cassete is not used (I'm assuming that it's because the code to connect to the ExAPI is not inside the VCR.use_cassete scope, but in a method inside the controller.

Is that a way to do this?

Thanks


Solution

  • Ok, my mistake. I forgot to initialize VCR:

    VCR.configure do |config|
      config.cassette_library_dir = "fixtures/vcr_cassettes"
      config.hook_into :webmock
    end