1

I'm new to Rails / open source development and I'm using Test Unit to write integration tests in conjunction with Capybara / WebDriver

How do I ensure that the Rails server is both running AND ready, prior to executing my integration tests?

I tried the following in my test_helper.rb:

server_running = false
path = File.join(Rails.root, "tmp", "pids", "server.pid")
if  FileTest.exist? path
  begin
    pid = File.read(path).to_i
    server_running = true if Process.getpgid pid
  rescue Errno::ESRCHfound
  end
end

if !(server_running)
  puts "Starting Rails Server"
  system "rails s"
else
  puts "Rails Server already started"
end

Problem is the tests never run because technically "system 'rails s'" never completes

So I next tried detached mode via "rails s -d" but this doesn't work for me (raised as separate question elsewhere on SO) and the server.pid is not present in the tmp folder either

So last I tried system "rails s &". Problem with this is, my unit tests start executing immediately before the Rails Server is actually ready. I guess what I really need is a way to start the Rails Server and figure out (either by polling or some callback) when it's actually ready to start accepting requests

Appreciate any advice and guidance on best practices regarding this. I've been recommended using Spork further down the road for CI but for now, I'd like to just stick to the tools I have (TestUnit, Capybara, WebDriver) unless absolutely necessary

EDIT: in my test_helper.rb I have the following at the end of the file

class ActionDispatch::IntegrationTest
  include Capybara::DSL

  def setup
    Capybara.default_driver = :webkit
    Capybara.run_server = true 
    Capybara.server_port = 3001
    Capybara.app_host = "http://localhost:3001"
  end


  def teardown
    Capybara.reset_sessions!
  end
end

One of my tests merely has visit('/linkedin/importprofile') - doing so yields the following exception:

  1) Error:
test_should_display_previously_imported_LinkedIn_data_in_Basic_Profile_fields(LinkedinTest):
Capybara::Webkit::InvalidResponseError: Unable to load URL: http://localhost:3001/linkedin/importprofile because of error loading http://localhost:3001/linkedin/importprofile: Unknown error
    /var/lib/gems/1.9.1/gems/capybara-webkit-1.0.0/lib/capybara/webkit/browser.rb:215:in `check'
    /var/lib/gems/1.9.1/gems/capybara-webkit-1.0.0/lib/capybara/webkit/browser.rb:152:in `command'
    /var/lib/gems/1.9.1/gems/capybara-webkit-1.0.0/lib/capybara/webkit/browser.rb:18:in `visit'
    /var/lib/gems/1.9.1/gems/capybara-webkit-1.0.0/lib/capybara/webkit/driver.rb:29:in `visit'
    /var/lib/gems/1.9.1/gems/capybara-2.1.0/lib/capybara/session.rb:193:in `visit'
    /var/lib/gems/1.9.1/gems/capybara-2.1.0/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
    /home/blue18hutthutt/Sites/pd_frontend/test/integration/linkedin_test.rb:31:in `block in <class:LinkedinTest>'
4

1 回答 1

3

您永远不需要手动启动 Rails 服务器来运行测试。需要时,驱动程序将自动启动并在测试运行完成时将其关闭。默认的 Rack::Test 驱动程序甚至不需要服务器进程,因为它直接与 Rack 接口。

如果您在删除手动服务器启动代码后仍然遇到问题,我会首先确保您已按照 README 的本节所述设置 Capybara 以使用 Test::Unit 。(编辑:虽然看起来你已经手动指定server_port并且app_host可能会干扰某些东西,如下面的评论中所讨论的。)

于 2013-09-30T22:52:50.870 回答