If I call url_for
within a feature spec, it returns an absolute URL starting with http://www.example.com/. Capybara will happily attempt to load pages on that site, but that has nothing to do with my app. Here are minimal steps to reproduce the issue:
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"
Run 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 removed when 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 the example.com page pop up in a browser. I have verified this behavior back to Rails 3.2.17.
Why is this happening, and is there a way to get URLs for the app being tested instead of example.com?
Edit: some things I have found looking into this more:
ActionDispatch::Routing::UrlFor.url_for is called from RSpec examples. It has only_path
defaulting to false.
ActionView::RoutingUrlFor is the version you get in, say, a view. It has only_path
defaulting to true, which works much better.
This commit to the rspec-rails gem probably caused the problem, by adding www.example.com
as the default host. There is no explanation anywhere about why this host is an appropriate/useful choice.
visit thing_url
). I found github.com/jnicklas/capybara/issues/306 where jeromelefeuvre says thatthing_path
should be used in place ofthing_url
. I changed to the_path
version and it is working. – Arlinearlington