VCR在第二次尝试运行测试时无法处理请求(第一次尝试通过)

rub*_*bie 4 ruby ruby-on-rails vcr

我已经设置了VCR,并且可以在我编写的无问题的多个测试上运行。我尝试写入的最新测试将在首次运行时通过,但在此之后仍将失败,除非删除卡带。该测试的代码是:

it "doesn't blow up if a user doesn't have billing info" do
    VCR.use_cassette('tax_reconciler/no_method_error')do
      user_guid = rand(10000000)
      CreateRecurlyTestData.create_account(user_guid, nil, nil)
      tax_reconciler = TaxReconciler.new
      new_tax_amount = rand(100000)
      user = create_test_user(:guid => user_guid)
      expect(tax_reconciler.update_tax_amount(user, new_tax_amount)).to_not raise_error
    end
  end
Run Code Online (Sandbox Code Playgroud)

错误消息如下:

失败/错误:CreateRecurlyTestData.create_account(user_guid,nil,nil)VCR ::错误:: UnhandledHTTPRequestError:

   ================================================================================
   An HTTP request has been made that VCR does not know how to handle:
     GET https://2b64d08ef45c446dbba75720a37b7d41:@api.recurly.com/v2/accounts/3276643

   VCR is currently using the following cassette:
     - /Users/Evan/dish/stallone/fixtures/vcr_cassettes/tax_reconciler/no_method_error.yml
     - :record => :once
     - :match_requests_on => [:method, :uri]

   Under the current configuration VCR can not find a suitable HTTP interaction
   to replay and is prevented from recording new requests. There are a few ways
   you can deal with this:

     * If you're surprised VCR is raising this error
       and want insight about how VCR attempted to handle the request,
       you can use the debug_logger configuration option to log more details [1].
     * You can use the :new_episodes record mode to allow VCR to
       record this new request to the existing cassette [2].
     * If you want VCR to ignore this request (and others like it), you can
       set an `ignore_request` callback [3].
     * The current record mode (:once) does not allow new requests to be recorded
       to a previously recorded cassette. You can delete the cassette file and re-run
       your tests to allow the cassette to be recorded with this request [4].
     * The cassette contains 4 HTTP interactions that have not been
       played back. If your request is non-deterministic, you may need to
       change your :match_requests_on cassette option to be more lenient
       or use a custom request matcher to allow it to match [5].

   [1] https://www.relishapp.com/vcr/vcr/v/2-9-3/docs/configuration/debug-logging
   [2] https://www.relishapp.com/vcr/vcr/v/2-9-3/docs/record-modes/new-episodes
   [3] https://www.relishapp.com/vcr/vcr/v/2-9-3/docs/configuration/ignore-request
   [4] https://www.relishapp.com/vcr/vcr/v/2-9-3/docs/record-modes/once
   [5] https://www.relishapp.com/vcr/vcr/v/2-9-3/docs/request-matching
Run Code Online (Sandbox Code Playgroud)

规范帮助器中唯一的配置选项是:

VCR.configure do |c|
    c.cassette_library_dir = 'fixtures/vcr_cassettes'
    c.hook_into :webmock # or :fakeweb
  end
Run Code Online (Sandbox Code Playgroud)

Jes*_*per 6

您的VCR设置以录制模式配置:oncehttps : //www.relishapp.com/vcr/vcr/v/1-8-0/docs/record-modes/once

简而言之,它仅在没有录像带时记录请求。对于后续运行,它将仅读取记录的请求,因此与任何记录的请求都不匹配的请求将引发此错误。

这种模式下出现问题的一种常见方法是删除卡带,并且只运行一个规格,而不是整个套件。宾果游戏-您的卡带已创建,除该特定规格中的一个请求外,所有其他请求都将无法识别。

此模式的一个好处是您可以捕获虚假请求-如果您知道卡带包含所有有效请求,则可以使用此模式捕获由于错误请求导致的错误。

如果要更加灵活,可以使用录制模式new_episodes。在这种模式下,识别的请求将从录像带中重放,未识别的请求将被执行和记录。

  • 添加`:record =>:new_episodes`会绕过错误,但可能不是您想要的。您正在使用VCR进行快速的确定性测试,但是使用`:record =>:new_episodes`意味着当VCR找不到匹配的请求时,它将允许该请求并将其记录下来。如果您更改了代码以触发新请求,那很好,但是在您的情况下,听起来好像是在重新运行测试时发生的。VCR无法与先前的测试相匹配,可能是因为您的请求是不确定的。在每个测试运行中,“ 2b64d08ef45c446dbba75720a37b7d41”和“ 3276643”零件是否相同? (3认同)