VCR无法记录后续请求

问题描述

我有一个系统规格,其受测代码发出2个外部HTTP请求。

我将整个测试用% Error: Unrecognized object type. 进行包装,但是无论出于什么原因,VCR都只记录我的两个请求中的第一个

这是我的考试。

VCR.use_cassette

这是要录制的卡带。如您所见,仅代表一个请求。

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

第二次运行测试时出现的错误是:

---
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","city": "FLEMINGTON","state":
        "NJ","telephone_number": "908-237-0200","fax_number":
        "908-237-0210"}],"taxonomies": [{"code": "207Q00000X","desc": "Family Medicine","primary": true,"license": "25MA05588600"}],"identifiers":
        []}]}'
    http_version:
  recorded_at: Thu,08 Oct 2020 13:19:01 GMT
recorded_with: VCR 5.0.0

我不明白为什么VCR没有记录第二个请求。非常感谢您的帮助。

解决方法

有趣的是,该问题似乎是由竞争状况引起的。我可以通过在测试末尾添加df %>% group_by(X1) %>% summarise(across(where(is.numeric),~ weighted.mean(.x,wgt),.names = "weighted_mean_{.col}")) 来解决此问题,如下所示。

sleep