【问题标题】:VCR failing to record subsequent requestsVCR 未能记录后续请求
【发布时间】:2020-10-08 14:01:46
【问题描述】:

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

我正在用VCR.use_cassette 结束我的整个测试,但无论出于何种原因,VCR 只记录了我的两个请求中的第一个。

这是我的测试。

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", "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

我第二次运行测试时得到的错误是:

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.

我不明白为什么 VCR 没有录制第二个请求。非常感谢任何帮助。

【问题讨论】:

    标签: rspec vcr


    【解决方案1】:

    有趣的是,问题似乎是由竞争条件引起的。我可以通过在测试末尾添加sleep 来修复它,如下所示。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 2023-04-07
      • 2013-10-18
      • 1970-01-01
      • 2016-09-11
      • 2018-02-08
      • 1970-01-01
      相关资源
      最近更新 更多