If I call url_for in the spec specification, it returns an absolute URL starting with http://www.example.com/ . Capybara will happily try to load pages on this site, but this has nothing to do with my application. The following are the minimum steps to reproduce the problem:
Start with this gemfile:
source 'https://rubygems.org' gem "sqlite3" gem "jquery-rails" gem "draper" gem "rails", '4.1.0' gem "therubyracer" gem "uglifier" gem "rspec-rails" gem "capybara" gem "poltergeist" gem "launchy"
Do the following:
bundle rails new myapp -O cd myapp rm Gemfile Gemfile.lock rails generate controller Test test rails generate rspec:install mkdir spec/features
Comment out the lines in spec/spec_helper.rb that say they should be deleted if you are not using ActiveRecord, and then create spec/features/feature_spec.rb with the following contents:
require 'capybara/poltergeist' Capybara.configure do |config| config.javascript_driver = :poltergeist end require 'spec_helper' describe "nothing", js: true do specify do visit(url_for(controller: :test, action: :test)) save_and_open_page end end
Finally, run rake spec and you will see that the example.com page appears in the browser. I tested this behavior on Rails 3.2.17.
Why is this happening, and is there a way to get the URLs for the application under test instead of example.com?
Edit: some things I found in this in more detail:
ActionDispatch :: Routing :: UrlFor.url_for is called from RSpec examples. It has only_path default value is false.
ActionView :: RoutingUrlFor is the version that you enter, say, in the view. It has only_path default value is true, which works much better.
This commit in gspec-rails probably caused the problem by adding www.example.com as the default host. There is no explanation why this host is a suitable / useful choice.