My specs will pass in master branch. If I create a new branch and modify some code completely unrelated to the subscriptions, they'll fail with this. The only way I can get them to pass is to change my vcr.rb to have :record => :new_episodes.
If I leave that option on, then almost every time my specs run I have new modified data files for cassettes that end up being committed which really dilute the logs for Git.
Any suggestions on how to handle this? A lot of the specs that break are based on this matcher:
describe "#change_plan_to", vcr: {match_requests_on: [:method, :uri, :body]} do
Run Code Online (Sandbox Code Playgroud)
Is this matcher too open to changes? I wasn't able to get the specs to pass any other way with stripe api calls.
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>'
Run Code Online (Sandbox Code Playgroud)
I figured out some more. The specs only break when I add a new spec to the stack. How come they break when more things are added to the suite?
您所看到的行为表明,用于匹配请求的属性之一是不确定的,并且每次运行测试时都会发生变化。您提到使用该match_requests_on: [:method, :uri, :body]选项 - 我猜它是body. 请记住,VCR 的内置正文匹配器会进行直接body_string == body_string比较,并且很容易出现正文在语义上等效但不是同一个字符串的情况。例如,像{"a": 1", "b": 2}vs的 JSON 字符串{"b": 2, "a": 1}。
我的建议是完全不匹配body:如果您在某些情况下需要它,它就在那里,但是如果您更宽松地匹配,VCR 通常可以正常工作,因为它按照最初发生的顺序记录 HTTP 交互,然后在播放期间,它会回放第一个未使用的匹配交互——如果你的测试按照最初的顺序发出请求,将导致为每个请求回放正确的响应。
为了更深入地了解正在发生的事情,您可以使用调试记录器选项,它会在尝试匹配时为您提供详细的输出,以显示它为什么正在做它正在做的事情。