Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

I'm reading through Rails 4 in Action. I'm getting the aforementioned error while testing with Rspec and Capybara. My viewing_projects_spec.rb looks like this:

require 'spec_helper'

feature "Viewing projects" do
  scenario "Listing all projectcs" do
    project = FactoryGirl.create(:project, name: "TextMate 2")
    visit '/'
    click_link 'TextMate 2'
    expect(page.current_url).to eql(project_url(project))
  end
end

The rest of the error says Failure/Error: expect(page.current_url).to eql(project_url(project)).

I did some googling and people say to place the following inside config/environments/development.rb:

Rails.application.routes.default_url_options[:host] = 'localhost:3000'

Unfortunately this doesn't do anything.

like image 443
Mike Glaz Avatar asked Apr 26 '13 04:04

Mike Glaz


1 Answers

The host statement should be put into config/environments/test.rb instead of development.rb, because you are running tests.

Add

The reason you see 'example.com' is Capybara set this as default host. To change that, just add the following line in spec_help after require 'capybara'

Capybara.default_host = 'localhost:3000'

Ref: https://rspec.lighthouseapp.com/projects/16211-cucumber/tickets/206

Then you urls should match what you've set in test.rb.

Anyway, I would prefer not to have that trouble to use current_path directly.

like image 121
Billy Chan Avatar answered Oct 22 '22 00:10

Billy Chan