gpt4 book ai didi

rspec - VCR 未处理的 http 请求错误

转载 作者:行者123 更新时间:2023-12-03 22:50:28 28 4
gpt4 key购买 nike

我的规范将通过主分支。如果我创建一个新分支并修改一些与订阅完全无关的代码,他们将因此失败。我能让他们通过的唯一方法是将我的 vcr.rb 更改为 :record => :new_episodes .

如果我保留该选项,那么几乎每次我的规范运行时,我都会为最终被提交的盒式磁带创建新的修改数据文件,这实际上稀释了 Git 的日志。

有关如何处理此问题的任何建议?许多破坏的规范都基于这个匹配器:

  describe "#change_plan_to", vcr: {match_requests_on: [:method, :uri, :body]} do

这个匹配器对变化太开放了吗?我无法通过 strip api 调用使规范通过任何其他方式。
Failure/Error: @subscription.create_stripe_customer
VCR::Errors::UnhandledHTTPRequestError:


================================================================================
An HTTP request has been made that VCR does not know how to handle:
POST https://api.stripe.com/v1/customers

VCR is currently using the following cassette:
- /Users/app/spec/data/Subscription/_change_plan_to/stripe_customer_subscription_plan_/name/.json
- :record => :once
- :match_requests_on => [:method, :uri, :body]

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 109 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-5-0/docs/configuration/debug-logging
[2] https://www.relishapp.com/vcr/vcr/v/2-5-0/docs/record-modes/new-episodes
[3] https://www.relishapp.com/vcr/vcr/v/2-5-0/docs/configuration/ignore-request
[4] https://www.relishapp.com/vcr/vcr/v/2-5-0/docs/record-modes/once
[5] https://www.relishapp.com/vcr/vcr/v/2-5-0/docs/request-matching
================================================================================
# ./app/models/subscription.rb:83:in `create_stripe_customer'
# ./spec/models/subscription_spec.rb:68:in `block (3 levels) in <top (required)>'
# -e:1:in `<main>'

我想出了更多。只有当我向堆栈添加新规范时,规范才会中断。当更多的东西添加到套件中时,它们怎么会坏掉?

最佳答案

您所看到的行为表明,用于匹配请求的属性之一是不确定的,并且每次运行测试时都会发生变化。您提到使用 match_requests_on: [:method, :uri, :body]选项 -- 我猜是 body .请记住,VCR 的内置 body 匹配器会直接执行 body_string == body_string比较,并且很容易出现主体在语义上等效但不是相同字符串的情况。例如,像 {"a": 1", "b": 2} 这样的 JSON 字符串对比 {"b": 2, "a": 1} .

我的建议是不匹配 body完全:如果您在某些情况下需要它,它就在那里,但是如果您更宽松地匹配,则 VCR 通常可以正常工作,因为它按照最初发生的顺序记录 HTTP 交互,然后在播放期间,它会播放第一个未使用的匹配的交互——如果您的测试以与最初相同的顺序发出请求,将导致为每个请求播放正确的响应。

要更深入地了解正在发生的事情,您可以使用 debug logger选项,它将在尝试匹配时为您提供详细的输出,以显示它为什么在做它正在做的事情。

关于rspec - VCR 未处理的 http 请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17822820/

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