如何阻止 VCR 覆盖盒式磁带中的 erb

发布于 2024-11-16 04:11:25 字数 401 浏览 3 评论 0原文

我们最近开始使用 VCR 在我们的 Cucumber 测试中存根请求。我们使用 cucumber 表来描述不同类型的请求,并使用 gherkin 将它们存储为变量。我们的盒式磁带文件都已重新设计以包含 erb,以便我们可以替换要测试的请求的值。

问题是,每当有新请求时,VCR 都会记录新请求,并覆盖(删除)磁带中的所有 erb,将其替换为该示例中插入的请求。每次我们运行值发生更改的请求(例如,我们从正在对话的 API 收到的时间戳值)时,所有 erb 都需要复制回卡带文件中。这很令人沮丧,因为测试一直在运行。

有谁知道为什么 VCR 在录制新回复时会去掉 erb ?有什么解决方法吗?有什么方法可以在插值之前保存模板吗?

We've recently started using VCR to stub requests in our cucumber tests. We're using cucumber tables to describe different kinds of requests, and storing them as variables with gherkin. Our cassette files have all been reworked to include erb, so that we can substitute in the values for the request we want to test.

The problem is that whenever there is a new request, VCR records the new requests and also overwrites (removes) all the erb from the cassette, replacing it with the request as interpolated for that example. Every time we run requests where a value has changed (say, the value of the timestamp we receive from the API we're talking to), all the erb needs to be copied back in to the cassette file. This is frustrating, since tests are run all the time.

Does anyone know why VCR strips the erb out when recording new responses? Any idea of a workaround? Is there any way to save back a template before it's interpolated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小情绪 2024-11-23 04:11:25

自动将对盒式磁带的手动更改与新录制的盒式磁带合并是一个难题,在我看来,最好留给专门设计用于处理文本文档历史记录并管理它们合并的工具(即源代码控制系统) 。 ERB 让它变得更加困难:虽然您可能只是使用 ERB 来插入变量,但可以使用任何有效的 ruby​​。 ERB 中可能存在循环、条件等。 VCR 没有办法自动合并这些东西。

一些建议:

如果这些东西不能满足您的需求,我当然愿意接受如何改进 VCR 的想法。

Automatically merging your manual changes to the cassette with a newly recorded cassette is a difficult problem that, in my opinion, is best left to tools that are specifically designed to handle the history of text documents and manage merging them (i.e. your source control system). ERB makes it even more difficult: while you may just be using ERB to interpolate variables, any valid ruby can be used. There may be loops, conditionals and more in the ERB. There's no way for VCR to automatically merge this stuff.

A couple suggestions:

  • Use your cassettes in a more granular fashion so that they do not get overridden frequently. I tend to use a casette for each HTTP request or logical group of HTTP requests.
  • Use the :once record mode (the recent default) to prevent existing cassettes from being overriden.
  • Take a look at the sensitive data filtering feature of VCR. This is far less flexible than ERB (i.e. you can't use loops, conditionals, or any arbitrary ruby code), but for simple variable intepolation, it works really, really well. Rather than using ERB, it uses a placeholder string--you might use a string like <PASSWORD> to take the place of a password, for example. VCR automatically takes care of inserting the placeholder text when it records the cassette, and replacing it with the correct real value on playback.
  • If that doesn't meet your needs, the before_record and before_playback hooks should give you all the power you need to do automatic interpolation (or even ERB snippet insertion) when a cassette is recorded. It's basically how the filtering works--see here.

I'm certainly open to ideas for how to improve VCR if these things don't meet your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文