2013-08-25 32 views
15

我正在關注的RoR Tutorial,我被困在Listing 9.15如何解決「缺少主機鏈接到!請提供:主機參數」? (ROR)

我收到以下錯誤運行「捆綁高管rspec的投機/」後:

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: "[email protected]") } 
     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 

我不知道如何解決此問題,以便測試通過。如何我解決了嗎?有人可以解釋發生了什麼問題嗎? (根據教程,測試應通過)。

回答

21

最後我只是用:而不是

root_path 

root_url 
+1

您是否將您的代碼與[參考實現](https://github.com/railstutorial/sample_app_rails_4)進行了比較?您列出的代碼似乎在我的系統上正常工作。 – mhartl

+0

對我來說,它也是這個答案和@AmanGarg的答案,因爲我在我的開發環境中更改了localhost:3000的url,並且我從未在我的測試env –

+0

中做過相同的更改。沒有改變任何東西。剛剛停止了服務器並重新啓動它。它的工作。 – Emanuel

61

問題可能是您尚未爲測試環境定義default_host。定義default_host的配置/環境/ test.rb裏面是這樣的:

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

仍然收到相同的錯誤。這似乎沒有任何區別。 – Sheldon

+3

注意更改配置後重新啓動rails server。 –

+2

添加'Rails.application.routes。'test.rb'結尾處的default_url_options [:host] ='localhost:3000'。 (在'do ... end'塊之後) –

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

您可以通過使用skip_confirmation來避免此錯誤!方法。

user = User.new(email: "[email protected]", password: "1234pass5678word") 
user.skip_confirmation! 
user.save 
user 
2

對於像OP例子中顯示的控制器測試那樣的測試,需要另一個設置來解決這個錯誤。 Kesha Antonov提到,你可以使用路由的default_url_options。通過添加以下設置你的配置/環境/ test.rb的最後一行(結束後),你的測試將使用給定的建立網址的主機:

Rails.application.routes.default_url_options[:host] = 'lvh.me:3000' 

正如OP中提到另一個答案,你可以改爲路徑Named Route當錯誤來自名爲路由的URL。

如果要設置郵件程序預覽的默認主機,您需要其他答案中提到的action_mailer.default_url_options。

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

請參閱生成的URL在Action Mailer API文檔有關action_mailer設置的詳細信息。

相關問題