【问题标题】:How do I resolve "Missing host to link to! Please provide the :host parameter"? (RoR)如何解决“缺少要链接的主机!请提供 :host 参数”? (罗尔)
【发布时间】:2013-08-25 20:37:39
【问题描述】:

我正在关注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

我不知道如何解决这个问题,所以测试通过了。我如何解决它?有人可以解释发生了什么问题吗? (根据教程,测试应该通过)。

【问题讨论】:

    标签: ruby-on-rails ruby rspec capybara ruby-on-rails-4


    【解决方案1】:

    问题可能是你没有为测试环境定义default_host。在 config/environments/test.rb 中定义 default_host,如下所示:

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

    【讨论】:

    • 仍然遇到同样的错误。这似乎没有任何区别。
    • 请注意在更改配置后重新启动 Rails 服务器。
    • test.rb末尾添加Rails.application.routes.default_url_options[:host] = 'localhost:3000'。 (do ... end 块之后)
    【解决方案2】:

    最后我只是用了:

    root_path
    

    代替:

    root_url
    

    【讨论】:

    • 您是否将您的代码与reference implementation 进行了比较?您列出的代码在我的系统上似乎可以正常工作。
    • 对我来说也是这个答案和@AmanGarg 的答案,因为我在我的开发环境中更改了 localhost:3000 的 url 并且我从未在我的测试环境中进行相同的更改
    • 有同样的错误。没有改变任何东西。刚刚停止服务器并再次重新启动它。它奏效了。
    • 不是问题的答案,不应该这样标记。请查看显示如何在特定于环境的配置文件中设置默认 url 选项的实际答案。这个“答案”只是导致将 url 更改为路径,例如而不是在您的电子邮件中使用“cnn.com”,您将使用“/”。是的,一个正斜杠。可能不是你想要的。这是更关心“测试通过”而不是实际解决问题的表现。
    • 这确实回答了使用 rspec 时“如何解决“缺少主机链接到...”的问题,因为它解决了对主机的需求。其他答案也没有提供解决方案因为它是控制器测试,而不是邮件测试。
    【解决方案3】:

    对于像 OP 示例中显示的控制器测试这样的测试,需要另一个设置来解决此错误。 Kesha Antonov 提到,您可以改用路由的 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 文档中的生成 URL。

    【讨论】:

      【解决方案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'
      

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 2019-12-11
          • 2016-07-08
          • 2018-11-08
          • 1970-01-01
          • 2011-05-06
          • 1970-01-01
          • 1970-01-01
          • 2017-12-31
          • 2014-04-12
          相关资源
          最近更新 更多