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

╄→гoц情女王★ 提交于 2020-01-14 09:42:50

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/16228731/missing-host-to-link-to-please-provide-the-host-parameter-set-default-url-opt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!