VCR failing to record subsequent requests

191 views Asked by At

I have a system spec whose code under test makes 2 external HTTP requests.

I'm wrapping my whole test with VCR.use_cassette, but for whatever reason VCR is only recording the first of my two requests.

Here's my test.

RSpec.describe 'Adding external physician', type: :system do
  it 'works' do
    VCR.use_cassette 'correspondence/external_physicians/add_external_physician_from_npi', record: :all do
      create(:state, abbreviation: 'NJ')

      appointment = create(:appointment)
      appointment.clinical_data.sign!(appointment.physician.user)
      login_as(appointment.physician.user)

      visit patient_chart_appointments_path(appointment.patient)
      find('.visit-summary-letter-link').click
      typeahead_fill_in 'Add corresponding physician (from NPI registry)', with: 'joel fuhrman'
      expect(page).to have_content('JOEL FUHRMAN')
      expect(page).to have_content('External Physicians')
    end
  end
end

Here's the cassette that gets recorded. As you can see, only one request is represented.

---
http_interactions:
- request:
    method: get
    uri: https://npiregistry.cms.hhs.gov/api/?address_purpose=&first_name=joel&last_name=fuhrman&version=2.1
    body:
      encoding: US-ASCII
      string: ''
    headers:
      Accept-Encoding:
      - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
      Accept:
      - "*/*"
      User-Agent:
      - Ruby
  response:
    status:
      code: 200
      message: OK
    headers:
      Date:
      - Thu, 08 Oct 2020 13:19:01 GMT
      Content-Type:
      - application/json
      Strict-Transport-Security:
      - max-age=31536000; includeSubDomains
      Set-Cookie:
      - TS017b4e40=01acfeb9486c77084e91ec07df37f7fe4c6d3e934aa2f03def62ad36d811abf2085bd35b9ce0c6d0e8c42fae1a2dd6de9d651c8d0e;
        Path=/; Domain=.npiregistry.cms.hhs.gov
      Transfer-Encoding:
      - chunked
    body:
      encoding: UTF-8
      string: '{"result_count":1, "results":[{"enumeration_type": "NPI-1", "number":
        1386765287, "last_updated_epoch": 1183852800, "created_epoch": 1175472000,
        "basic": {"name_prefix": "DR.", "first_name": "JOEL", "last_name": "FUHRMAN",
        "middle_name": "H", "credential": "MD", "sole_proprietor": "YES", "gender":
        "M", "enumeration_date": "2007-04-02", "last_updated": "2007-07-08", "status":
        "A", "name": "FUHRMAN JOEL"}, "other_names": [], "addresses": [{"country_code":
        "US", "country_name": "United States", "address_purpose": "LOCATION", "address_type":
        "DOM", "address_1": "4 WALTER E FORAN BLVD", "address_2": "SUITE 409", "city":
        "FLEMINGTON", "state": "NJ", "postal_code": "088224664", "telephone_number":
        "908-237-0200", "fax_number": "908-237-0210"}, {"country_code": "US", "country_name":
        "United States", "address_purpose": "MAILING", "address_type": "DOM", "address_1":
        "4 WALTER E FORAN BLVD", "address_2": "SUITE 409", "city": "FLEMINGTON", "state":
        "NJ", "postal_code": "088224664", "telephone_number": "908-237-0200", "fax_number":
        "908-237-0210"}], "taxonomies": [{"code": "207Q00000X", "desc": "Family Medicine",
        "primary": true, "state": "NJ", "license": "25MA05588600"}], "identifiers":
        []}]}'
    http_version:
  recorded_at: Thu, 08 Oct 2020 13:19:01 GMT
recorded_with: VCR 5.0.0

The error I get when I run the test a second time is:

VCR::Errors::UnhandledHTTPRequestError:                                                  
                                                                                                                                                                                         
                                                                                                                                                                                         
   ================================================================================                                                                                                      
   An HTTP request has been made that VCR does not know how to handle:                                                                                                                   
     GET https://npiregistry.cms.hhs.gov/api/?address_purpose=&number=1386765287&version=2.1                                                                                             
                                                                                                                                                                                         
   There is currently no cassette in use.

I don't understand why VCR isn't recording the second request. Any help is very much appreciated.

1

There are 1 answers

0
Jason Swett On

Interestingly, the problem seems to have been caused by a race condition. I was able to fix it by adding a sleep to the end of the test as follows.

RSpec.describe 'Adding external physician', type: :system do
  it 'works' do
    VCR.use_cassette 'correspondence/external_physicians/add_external_physician_from_npi' do
      create(:state, abbreviation: 'NJ')

      appointment = create(:appointment)
      appointment.clinical_data.sign!(appointment.physician.user)
      login_as(appointment.physician.user)

      visit patient_chart_appointments_path(appointment.patient)
      find('.visit-summary-letter-link').click
      typeahead_fill_in 'Add corresponding physician (from NPI registry)', with: 'joel fuhrman'
      expect(page).to have_content('JOEL FUHRMAN')
      expect(page).to have_content('External Physicians')

      # The following sleep prevents a race condition.
      # Without the sleep, a second HTTP request gets fired outside
      # the use_cassette block and an exception is raised.
      sleep(1)
    end
  end
end