Travis CI: 启动Rails服务器

7

我需要在Travis上启动一个Rails服务器来运行集成测试。 我已经将以下内容添加到配置文件中:

before_script:
  - psql -c 'create database scalia_test;' -U postgres
  - "bundle exec rails server -p 3000 &"

然而,Cypress 依然报错:

http://localhost:3000/users/sign_in
We attempted to make an http request to this URL but the request failed without a response.
We received this error at the network level:
  > Error: connect ECONNREFUSED 127.0.0.1:3000
Common situations why this would fail:
  - you don't have internet access
  - you forgot to run / boot your web server
  - your web server isn't accessible
  - you have weird network configuration settings on your computer
The stack trace for this error is:
Error: connect ECONNREFUSED 127.0.0.1:3000
    at Object.exports._errnoException (util.js:1024:11)
    at exports._exceptionWithHostPort (util.js:1047:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1150:14)

有人知道如何在Travis上启动Rails服务器吗?


尝试在启动服务器后添加 sleep 3 命令。同时确保设置了 RAILS_ENV=test 环境变量。 - Tarun Lalwani
有关此事有任何更新吗?我从未收到您的任何反馈。 - Tarun Lalwani
3个回答

4
你正在使用&将命令发送到后台,然后运行你的测试,对吗?
当你运行测试时,Travis仍在后台启动你的Rails服务器,因此会出现连接错误。我认为这与端口绑定无关。
为了解决这个问题,你应该在Rails启动后使用-d参数将其变为守护进程:
before_script:
  - psql -c 'create database scalia_test;' -U postgres
  - "bundle exec rails server -p 3000 -d"

rails server将会一直阻塞直到它监听到3000端口,然后将自己发送到后台并且继续运行你的脚本。

请注意,在你的测试运行完成后,你可以使用以下命令再次关闭rails server:

kill -9 $(cat tmp/pids/server.pid)

否则,你将会从Travis得到超时或其他错误。

0
尝试将Rails绑定到该端口的环回IP。
bundle exec rails s -p 3000 -b 127.0.0.1

-2

很可能该端口正在使用中。在终端中键入lsof -i :8000以查看该端口是否正在被使用。 您可以尝试使用不同的端口启动服务器,例如rails server -p 3050


我在询问Travis,而不是我的本地机器。我无法在终端中运行“lsof -i:8000”。 - Graham Slick
顺便说一下,你有没有意识到对那些试图帮助你的人进行负面评价将不利于未来获得帮助或解决问题。 - Imre Raudsepp
只是草草浏览问题并回答超出范围的内容,试图从SO上获取分数是没有帮助的,可能会混淆未来的读者,因此会被投票否决。 - Graham Slick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接