Search code examples
ruby-on-railsrspeccapybarapassenger

Rails 5: Using Capybara with Phusion Passenger


I am trying to run specs on a Rails 5 app. However, I am seeing the following LoadError:

Failure/Error: raise LoadError, 'Capybara is unable to load puma for its server, please add puma to your project or specify a different server via something like Capybara.server = :webrick.'

I am using the passenger gem for the production server. Is there a way in which I can use passenger for the Capybara tests as well? Thanks in advance


Solution

  • If you really want to use passenger to run your app while testing you will need to startup your app separately with passenger using the rails test environment (so it uses the test DB, etc) and then set

    Capybara.run_server = false
    Capybara.app_host = "http://<wherever the app is accessible>"
    

    for your tests. This tells Capybara not to bother with running the app itself and to just connect to the app you're already running. You will also need to use something like database_cleaner to handle database resetting between tests, and be very careful to make sure you don't have any leftover requests running at the end of each test.

    When running the tests with puma or webrick none of that is required (database_cleaner is generally required for rails < 5.1 but not 5.1+), because the web server is run in the same process (different threads) as the tests which allows Capybara to know when requests are still being processed and for Rails to share the DB connection with the tests. Overall you're just going to have a much smoother experience if you stick with puma or webrick for your tests.