gpt4 book ai didi

ruby-on-rails - VCR 在第二次尝试运行测试时抛出无法处理请求(第一次尝试通过)

转载 作者:数据小太阳 更新时间:2023-10-29 07:28:37 28 4
gpt4 key购买 nike

我设置了 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

错误信息如下:

失败/错误:CreateRecurlyTestData.create_account(user_guid, nil, nil) VCR::Errors::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

spec helper 中唯一的配置选项是:

VCR.configure do |c|
c.cassette_library_dir = 'fixtures/vcr_cassettes'
c.hook_into :webmock # or :fakeweb
end

最佳答案

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

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

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

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

如果你想更灵活,可以使用录制模式new_episodes。在此模式下,已识别的请求将从磁带中重播,未识别的请求将被执行并记录。

关于ruby-on-rails - VCR 在第二次尝试运行测试时抛出无法处理请求(第一次尝试通过),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25799165/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com