title: How do I start the rails server as part of my integration tests?
body: <p>I'm new to Rails / open source development and I'm using Test Unit to write integration tests in conjunction with Capybara / WebDriver</p>
  
  <p>How do I ensure that the Rails server is both running AND ready, prior to executing my integration tests?</p>
  
  <p>I tried the following in my test_helper.rb:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Problem is the tests never run because technically "system 'rails s'" never completes</p>
  
  <p>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</p>
  
  <p>So last I tried system "rails s &amp;". 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</p>
  
  <p>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</p>
  
  <p>EDIT: in my test_helper.rb I have the following at the end of the file</p>
  
  <pre><code>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
  </code></pre>
  
  <p>One of my tests merely has visit('/linkedin/importprofile') - doing so yields the following exception:</p>
  
  <pre><code>  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 &lt;module:DSL&gt;'
      /home/blue18hutthutt/Sites/pd_frontend/test/integration/linkedin_test.rb:31:in `block in &lt;class:LinkedinTest&gt;'
  </code></pre>