我正在关注RoR Tutorial,我被困在Listing 9.15
运行'bundle exec rspec spec /'后出现以下错误:
1) Authentication authorization as wrong user submitting a PATCH request to the Users#update action
Failure/Error: specify { expect(response).to redirect_to(root_url) }
ArgumentError:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
# ./spec/features/authentication_pages_spec.rb:79:in `block (5 levels) in <top (required)>'
我的身份验证测试代码是:
要求'spec_helper'
describe "Authentication", type: :request do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_content('Sign in') }
it { should have_title('Sign in') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_title('Sign in') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
#it { should have_title(user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Settings', href: edit_user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
end
end
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "in the Users controller" do
describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_title('Sign in') }
end
describe "submitting to the update action" do
before { patch user_path(user) }
specify { expect(response).to redirect_to(signin_path) }
end
end
end
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
before { sign_in user, no_capybara: true }
describe "visiting Users#edit page" do
before { visit edit_user_path(wrong_user) }
#it { should_not have_title(full_title('Edit user')) }
end
describe "submitting a PATCH request to the Users#update action" do
before { patch user_path(wrong_user) }
specify { expect(response).to redirect_to(root_url) }
end
end
end
end
我不知道如何解决这个问题,所以测试通过了。 如何 我如何解决?有人可以解释出了什么问题吗? (根据教程,测试应该通过)。
答案 0 :(得分:62)
问题可能是您没有为测试环境定义default_host。在config / environments / test.rb中定义default_host,如下所示:
config.action_mailer.default_url_options = {:host => "localhost:3000"}
答案 1 :(得分:21)
最后我刚刚使用了:
root_path
而不是:
root_url
答案 2 :(得分:4)
您可以在每个环境(开发,测试,制作)中设置default_url。
您需要进行这些更改。
config/environments/development.rb
config.action_mailer.default_url_options =
{ :host => 'your-host-name' } #if it is local then 'localhost:3000'
config/environments/test.rb
config.action_mailer.default_url_options =
{ :host => 'your-host-name' } #if it is local then 'localhost:3000'
config/environments/development.rb
config.action_mailer.default_url_options =
{ :host => 'your-host-name' } #if it is local then 'localhost:3000'
答案 3 :(得分:2)
对于OP示例中显示的控制器测试等测试,需要另一个设置来解决此错误。由Kesha Antonov提及,您可以使用Routes的default_url_options。通过在config / environments / test.rb的最后一行添加以下设置(在 end 之后),您的测试将使用给定的主机来构建URL:
Rails.application.routes.default_url_options[:host] = 'lvh.me:3000'
正如OP在另一个答案中所提到的,当错误来自名为route的URL时,您可以转而使用 Path Named Route。
如果要为邮件程序预览设置默认主机,则需要在其他答案中提及action_mailer.default_url_options。
config.action_mailer.default_url_options = { :host => "localhost:3000" }
有关action_mailer设置的详细信息,请参阅Action Mailer API文档中的生成网址。
答案 4 :(得分:-5)
您可以使用 skip_confirmation!方法来避免此错误。
user = User.new(email: "example@example.com", password: "1234pass5678word")
user.skip_confirmation!
user.save
user