ruby-on-rails-4 - Stubing google places call selenium v​​s capybara-webkit

标签 ruby-on-rails-4 integration-testing capybara-webkit webmock

我将 Capybaraselenium 和/或 capybara-webkit 驱动程序和 Webmock 一起用作 stub 框架。

当我使用 selenium (firefox) 运行 google map (地点)自动完成输入的集成测试时,浏览器对 google 进行了几次实际调用并接收了带有地点组件的 JSON。

当我更改为 capybara-webkit 驱动程序(muuuuch 更快)来执行相同操作时,我收到 webmock 消息,提示我应该 stub 外部请求。

有人可以解释一下为什么两者之间有区别吗?

注意:我设置了 WebMock.disable_net_connect!( :allow_localhost => true ) 以允许调用本地服务器...

编辑:

Tools like WebMock are great, but when testing JavaScript, it’s a seperate browser process that loads the page, and not your Ruby test process. That means that the request to your build server isn’t going through Net::HTTP; the requests are coming from Firefox or capybara-webkit, and those tools are gleefully unaware of your feeble attempts to reroute HTTP traffic

引自 http://robots.thoughtbot.com/using-capybara-to-test-javascript-that-makes-http

这解释了为什么某些对支付系统的调用(在 Rails Controller 中完成)需要 stub ,而 ajax 不需要 stub ,但它没有解释为什么 Firefox 到达 Google 而 webkit 要求 stub

解决方案:

TL;DR; 保留 :selenium 并在需要对外部 API 进行 ajax 调用的测试中进行真正的调用

我尝试使用用于 JS/浏览器外部调用的 puffing-billy 进行模拟,但是这个与 Webmock 不能“很好地配合”。我也有 25 个支付系统的 stub 已经用 Webmock 编写,所以我无法切换......

我也试过 VRC,它是一个记录框架,但这个也是用于你的应用程序(Ruby NET 库)进行的调用

最后,我只是决定继续运行这对需要使用 :selenium 驱动程序的 ajax API 响应的测试,该驱动程序启动 firefox 并真正调用 gMaps

#spec_helper
...
WebMock.disable_net_connect!( :allow_localhost => true )   # Ask to stub all requests except to localhost
...

# Use headless capybara-webkit that is way faster!
Capybara.javascript_driver = :webkit
config.before(:each, js: true) do
   ...
end

# And when you need :selenium and firefox mark the spec with :force_selenium => true
config.before(:each, force_selenium: true) do
   Capybara.current_driver = :selenium
end

最佳答案

这些警告不是来自 webmock,而是来自 capybara-webkit 本身,它希望您明确声明访问主机的行为,如下所示:

Capybara::Webkit.configure do |config|                                      
   config.allow_url("fonts.googleapis.com")
   config.allow_url("www.sandbox.paypal.com")
   config.allow_url("altfarm.mediaplex.com")
end

关于ruby-on-rails-4 - Stubing google places call selenium v​​s capybara-webkit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27938601/

相关文章:

ruby - Rails : I installed Ruby, 现在 "bundle install"不起作用

mysql - 尝试部署 Rails 应用程序时获取 ActiveRecord::AdapterNotSpecified: 'old_production' 数据库未配置

java - 如何解决 Spring Boot 上的 org.testcontainers.containers.ContainerFetchException?

Spring Boot : how to configure autowired WebTestClient

webkit - 在 Capybara-webkit 中 Stripe 结帐填写表格

css - Rails 4 样式表加载(twitter bootstrap 和更少)

ruby-on-rails - 了解 Ruby on Rails 中的类变量和方法

Spring SimpleJdbcTestUtils 示例

ruby - 从 span 标签中读取文本值

jquery - capybara 没有在模糊时触发 jquery 来设置自动保存