Notes to self

Following Rails redirects with Capybara

This a very short note on setting up Capybara to correctly follow redirects with Rails classic redirect_to @object pattern.

The problem why Capybara might not properly follow redirects in this case is that when redirect_to is called with ActiveRecord object argument an absolute URL is created and passed instead of relative one. So depending on the way of how Capybara server is started this might not work. To avoid this issue, one has to properly setup app_host and Capybara server settings (server_host and server_port):

# Your original Capybara setup
Capybara.javascript_driver = :poltergeist

# New server settings
Capybara.app_host = 'http://localhost:3002'
Capybara.server_host = 'localhost'
Capybara.server_port = '3002'

Here we use port 3002 for both port settings and Capybara redirects as expected!

Check out my book
Interested in Ruby on Rails default testing stack? Take Minitest and fixtures for a spin with my latest book.

Get Test Driving Rails while it's in prerelease.

by Josef Strzibny
RSS