带水豚 webkit 的录像机 [英] vcr with capybara-webkit

查看:65
本文介绍了带水豚 webkit 的录像机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 capybara-webkit 来测试与第三方网站的集成(我需要 javascript).

I'm using capybara-webkit to test integration with a third party website (I need javascript).

我想使用 vcr 来记录在集成测试期间发出的请求,但 capybara-webkit 不通过网络 http,因此 vcr 无法记录它们.我将如何为 vcr 编写一个允许我记录请求的适配器?

I want to use vcr to record requests made during the integration test but capybara-webkit doesn't go over net http so vcr is unable to record them. How would I go about writing an adaptor for vcr that would allow me to record the reqeusts?

推荐答案

不幸的是,VCR 与 capybara-webkit 非常不兼容.事实上,capybara webkit 正在使用 webkit,它在 c 中.作为 VCR 的基础的 Webmock 和 Fakeweb 只能用于 Ruby Web 请求.让两者协同工作可能是一项艰巨的任务.

Unfortunately, VCR is very much incompatible with capybara-webkit. The fact is that capybara webkit is using webkit, which is in c. Webmock and Fakeweb, which are the basis for VCR, can only be used for Ruby web requests. Making the two work together would likely be a monumental task.

我已经通过两种方式解决了这个问题:

I've solved this problem two ways:

第一个(hacky,但有效)是向仅包含在测试环境中的应用程序添加一个新的 javascript 文件.该文件存根了发出外部 Web 请求的 JS 类.除了这种方法的纯hackatude之外,它要求每次添加或更改新请求时,您还必须更改存根.

The first (hacky, but valid) is to add a new javascript file to the application that is only included in the test environment. This file stubs out the JS classes which make external web requests. Aside from the pure hackatude of this approach, it requires that every time a new request is added or changed you must change the stubs as well.

第二种方法是通过我自己的服务器路由所有外部请求,通过我的服务器有效地代理所有外部请求.这有一个巨大的缺点,即您必须对要消费的所有内容进行操作(您可以通过一些工作将其通用化).它还受到以下事实的影响:它可能会将完成请求的时间加倍.但是,由于现在请求是由 Ruby 发出的,因此您可以尽情使用 VCR.

The second approach is to route all external requests through my own server, effectively proxying all external requests through my server. This has the huge disadvantage that you have to have an action for everything you want to consume (you could genericize it, with some work). It also suffers from the fact that it could as much as double the time for the request to complete. However, since the requests are now being made by Ruby you can use VCR in all it's glory.

在我的情况下,方法#2 对我更有利,因为我需要 ruby​​ 来操作数据,以便我可以保持我的 javascript 源代码不可知.然而,我成功地使用方法#1 有一段时间了.

In my situations, approach #2 has been much more to my advantage thanks to the fact that I need ruby to manipulate the data so that I can keep my javascript source-agnostic. I was, however, using approach #1 for quite a while successfully.

这篇关于带水豚 webkit 的录像机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆