我该如何解决“缺少要链接到的主机!请提供:host参数”?(RoR)

18

我在跟随RoR教程学习,目前卡在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)>'

我的身份验证测试代码是:

require '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

我不知道如何解决这个问题,以便测试通过。我应该怎么解决呢?有人能解释一下出了什么问题吗?(根据教程,测试应该通过)。

5个回答

65

问题可能是你没有为测试环境定义default_host。在config/environments/test.rb中定义default_host,像这样:

config.action_mailer.default_url_options = {:host => "localhost:3000"}

16
仍然得到相同的错误。这似乎没有产生任何区别。 - Sheldon
4
修改配置后需要重新启动Rails服务器。 - Yannick Schuchmann
3
请将 Rails.application.routes.default_url_options[:host] = 'localhost:3000' 添加到 test.rb 的末尾(在 do ... end 块之后)。 - Kesha Antonov

23

最终我只使用了:

root_path

而不是:

root_url

1
你有没有将你的代码与参考实现进行比较?你列出的代码在我的系统上似乎可以正常工作。 - mhartl
对我来说,这个答案和@AmanGarg的答案都很有用,因为我在开发环境中将URL从localhost:3000更改了,但我从未在测试环境中进行相同的更改。 - Jeff Finn
出现了相同的错误。没有更改任何内容。只是停止了服务器,然后重新启动它。然后它就可以工作了。 - emi
4
这不是对问题的回答,不应该被标记为回答。请查看真正的答案,了解如何在特定环境配置文件中设置默认的URL选项。这个“答案”只会导致URL被更改为路径,例如,在电子邮件中,您将只会看到"/",而不是"http://cnn.com/"。这很可能不是您想要的。这表明关注“测试通过”比实际解决问题更重要。 - Michael Chaney
1
这确实回答了关于使用rspec时如何解决“缺少主机链接到...”的问题,因为它避免了需要主机的需求。此外,其他答案并没有提供解决方案,因为这是一个控制器测试,而不是一个邮件测试。 - DBrown

6
对于像OP示例中显示的控制器测试这样的测试,需要另一种设置来解决此错误。如Kesha Antonov所提到的,您可以改用Routes的default_url_options。通过在config/environments/test.rb的最后一行(在end之后)添加以下设置,您的测试将使用给定的主机来构建URL:
Rails.application.routes.default_url_options[:host] = 'lvh.me:3000'

如另一篇答案中由OP提到的,当错误来自于一个URL命名路由时,你可以转而切换到命名路由Path

如果您想为邮件预览设置默认主机,您将需要在其他答案中提到的action_mailer.default_url_options。

config.action_mailer.default_url_options = { :host => "localhost:3000" }

请参阅Action Mailer API文档中有关action_mailer设置的更多信息。

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'

-7

您可以通过使用 skip_confirmation! 方法来避免此错误。

user = User.new(email: "example@example.com", password: "1234pass5678word")
user.skip_confirmation!
user.save
user

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接