我想使用 rspec 结合 Faraday 和 Faraday_middleware 来测试 Rails 应用程序的 json 响应。要使用 Faraday,需要应用程序 URL。在测试中我想使用本地主机+端口号。
问题是,如何在 rspec 测试环境中获取或设置当前应用程序实例的端口号?
有类似的问题但没有满意的答案here https://stackoverflow.com/questions/6807268/rails-port-of-testing-environment and here https://stackoverflow.com/questions/6506343/having-a-default-port-when-running-tests-in-ruby-on-rails.
有任何想法吗?
我有同样的需求(Watir+RSpec),但问题是:测试期间默认没有运行机架堆栈......
我找到了这个解决方案:
In spec_helper.rb
:
HTTP_PORT = 4000
test_instance_pid = fork do
exec 'unicorn_rails -E test -p %d' % HTTP_PORT
end
at_exit do
Process.kill "INT", test_instance_pid
Process.wait
end
它为所有规范测试启动一次测试堆栈,并在最后终止它。(在此示例中,我使用的是 unicorn,但我们可以想象使用任何其他服务器)
在规范中,我重用了HTTP_PORT
构建 URL 的常量:
browser.goto "http://localhost:#{HTTP_PORT}/"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)