问题描述
我正在阅读Rails 4 in Action。在使用Rspec和Capybara进行测试时,出现上述错误。我的viewing_projects_spec.rb如下所示:
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
其余错误表示失败/错误:Expect(page.current_url).to eql(project_url(project))
。
我做了一些谷歌搜索,有人说要在config / environments / development.rb中放置以下内容:
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.
推荐答案
host语句应放在config / environments / test.rb中,而不是开发中.rb,因为您正在运行测试。
The host statement should be put into config/environments/test.rb instead of development.rb, because you are running tests.
看到 example.com的原因是Capybara将其设置为默认主机。要更改此设置,只需在需要'capybara'
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'
参考:
然后,您的网址应该与您在test.rb中设置的内容匹配。
Then you urls should match what you've set in test.rb.
无论如何,我希望不会遇到麻烦直接使用 current_path
。
Anyway, I would prefer not to have that trouble to use current_path
directly.
这篇关于主机链接丢失!请提供:host参数,设置default_url_options [:host],或将:only_path设置为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!