设置 Capybara 为其启动的服务器设置别名

Setting up Capybara to have an alias for the server it boots up

所以我有一些功能想要测试。

对于部分功能,我希望向其他主机发出请求。

当我在本地(在 Capybara 之外)测试我的代码时,我可以对一部分请求使用 localhost:3000,对另一部分请求使用 0.0.0.0:3000

对用水豚复制这种方法的最佳方法有什么想法吗?

如果我将默认主机设置为 localhost:3000 并自行启动服务器,它就可以工作。但还不能让它正常工作。

FWIW 我正在使用 poltergeist 作为 js 驱动程序。

控制应用程序在其启动的测试服务器下的 Capybara 设置是 Capybara.server_host(默认为 127.0.0.1)和 Capybara.server_port(默认为随机自由端口)。获得所需内容的一种方法是修复端口,然后选择一个域,将所有主机名的所有内容都解析为 127.0.0.1(可能在您的机器上 .test - 或者为您的 hosts 添加一个新名称解析为 127.0.0.1)

的文件
Capybara.server_port = 9999
visit('/') # will go to http://127.0.0.1:9999/
visit('http://127.0.0.1:9999')
visit('http://my_app.test:9999')

另一个选项是设置 Capybara.always_include_port = true 这将覆盖任何访问调用的端口,这些访问调用没有明确包含端口 Capybara 有 运行 它的服务器(因此可以仍然是水豚选择的随机港口)

Capybara.always_include_port = true
visit('/') # http://127.0.0.1:<server_port>/
visit('http://my_app.test') # http://my_app.test:<server_port>
visit('http://google.com') # http://google.com:<server_port>
visit('http://google.com:80/') # http://google.com