问题
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