当:js =>运行RSpec测试时,capybaraWebKit无法访问网页。真正 [英] capybaraWebKit is unable to visit webpages when running RSpec tests when :js => true

查看:75
本文介绍了当:js =>运行RSpec测试时,capybaraWebKit无法访问网页。真正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以前的RSpec集成测试中,:js => false(即JavaScript已关闭),我的测试运行得很好。但是,对于RSpec测试,其中:js => true(因此需要使用capybara WebKit),我始终收到以下错误:

On previous RSpec integration tests where :js => false (i.e., JavaScript was turned off), my tests were running just fine. However, for RSpec tests where :js => true (and therefore needing to use capybara WebKit), I consistently get the following error:

Unable加载URL:http://my.server:5000 / auth / identity,因为加载http://my.server:5000 / auth / identity:未知错误

请注意,这是在以下代码行上发生的:当:js => false时,该代码行之有效(这是登录用户的方法的一部分,这对两组集成都是必需的测试)。

Noted this is happening on a line of code that was working just fine when :js => false (it is part of a method that signs in a user, which is required for both sets of integration tests).

我尝试过的操作


  • 使用ufw(因为我在Ubuntu上运行),确保端口5000已打开。但是,无论如何我都不认为这是问题所在,因为当:js => false

  • 访问其他页面时,测试可以在其他测试中使用相同的端口正常运行例如, http://my.server:5000 / ),但是会出现相同的错误消息。

  • 我尝试使用 capybara WebKit文档的调试代码提供:

  • Using ufw (since I am running on Ubuntu), I made sure that port 5000 is open. However, I don't think that this was the problem anyway, since the tests were able to run fine using the same port in other tests when :js => false
  • Visiting other pages (e.g., http://my.server:5000/), but the same error message appears.
  • I tried using the debugging code that capybara WebKit documentation provides:

Capybara :: Webkit.configure do | config |
config.debug = true

我将其放在spec_helper中。 rb。但这最终给了我另一个错误消息,即:Capybara :: Webkit:Module 的 NoMethodError:undefined method configure'。

I put it in my spec_helper.rb. But this ends up giving me another error message, namely: NoMethodError: undefined method configure' for Capybara::Webkit:Module.


  • 我尝试停止并重新启动spring(使用 spring stop 春季重启),请按照这些说明进行操作。仍然没有任何工作。 (我尝试在两个命令之后都运行测试,但两次都仍然收到相同的错误消息)

  • I tried stopping and restarting spring (using spring stop and spring restart) following these instructions. Still nothing is working. (I tried running the tests after both commands, and still got the same error message both times)

关于我的配置的注释:


  • 当我收到该项目时,spec_helper.rb文件的水豚角为。 server_port = 5000 ,但我没有更改。然后,该变量将在所有 visit 方法中使用(例如,`visit http://my.server:# {Capybara.server_port} /)。

  • When I received the project, the spec_helper.rb file had Capybara.server_port = 5000, and I haven't changed that. This variable is then used in all of the visit methods (e.g., `visit "http://my.server:#{Capybara.server_port}/").

没有行包含 Capybara.app_host = ______

此网络应用程序包含三个子域,我需要访问这些子域: cms.my.server www.my.server admin.my.server 。 (请记住, my.server 是实际服务器名称的替代品,出于法律原因,我不能使用它)。在我的主机文件中,所有这3个都设置为解析为127.0.0.1。

This web app includes three subdomains which I will need to be able to access: cms.my.server, www.my.server, and admin.my.server. (Keep in mind that my.server is a stand-in for the actual server name, which I cannot use for legal reasons). All 3 of these are set to resolve to 127.0.0.1 in my hosts file.

还请注意,我的队友有得到了完全相同的测试。

Also note that my teammate has got the exact same test to work.

推荐答案

假设您使用标准的Capybara rspec配置,而 js:false (或未指定 js 元数据),则Capybara将使用rack_test驱动程序。 rack_test驱动程序完全忽略所有指定的主机名和端口,并将请求直接提交到rails应用程序路由处理程序。因此,如果您告诉它连接到 http://my.server:5000 ,它将直接进入目的地为 /的路由处理程序。 -因此,在指定 js:false 时,它可以工作的事实并不意味着端口5000实际上在做任何事情。

Assuming you are using the standard Capybara rspec configuration, when js: false is specified (or no js metadata is specified) then Capybara will be using the rack_test driver. The rack_test driver completely ignores all hostnames and ports specified and submits "requests" directly to the rails apps routing handlers. Because of this if you tell it connect to http://my.server:5000 it just goes straight to the routing handlers with a destination of / - so the fact that it works when js: false is specified does not in any way imply that port 5000 is actually doing anything.

要回答的问题是为什么Capybara尝试连接到端口5000。当测试Capybara正常使用的应用程序时,Capybara将在单独的线程中启动该应用程序并将其绑定在随机端口上为127.0.0.1。随机端口极不可能是5000,而且绝对不会连续运行多个。从这里开始,我们正在猜测领土(没有看到您的Capybara配置),但是您可能已经指定了 Capybara.app_host ='http://my.server:5000'仅当您将其组合为 Capybara.server_port = 5000 时才起作用,从而强制Capybara将其服务器绑定在端口5000上。首选方法是

The question to answer is why is Capybara is trying to connect to port 5000. When testing an app with normal Capybara usage, Capybara will start up the app in a separate thread and bind it to 127.0.0.1 on a random port. It's highly unlikely the random port would be 5000 and definitely wouldn't be multiple runs in a row. From here on we're in guessing territory (without seeing your Capybara config) but you probably have specified Capybara.app_host = 'http://my.server:5000' which would only work if you combined it Capybara.server_port = 5000, thereby forcing Capybara to bind its server on port 5000. The preferred option would be to do

Capybara.app_host = 'http://my.server'
Capybara.always_include_port = true

这将使水豚选择一个随机端口,然后将其添加到进行的每个访问调用中(注意:这全部假设'my.server'在您要测试的计算机上确实解析为127.0.0.1)。

which will let capybara pick a random port, but then add it into every visit call made (Note: this all assumes that 'my.server' does resolve to 127.0.0.1 on the machine you're testing on).

这篇关于当:js =>运行RSpec测试时,capybaraWebKit无法访问网页。真正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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