2013-08-25 12 views
15

Sto seguendo il RoR Tutorial e mi sono bloccato a Listing 9.15Come posso risolvere "Host mancante per il collegamento! Fornire il parametro host"? (RoR)

io ottenere il seguente errore dopo l'esecuzione 'fascio 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)>' 

Il mio codice di prova di autenticazione è:

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

Non so come risolvere questo problema in modo che il test passi. Come do Lo risolvo? Qualcuno potrebbe spiegare cosa sta andando storto? (Secondo il tutorial il test dovrebbe passare).

risposta

21

Alla fine ho appena usato:

root_path 

invece di:

root_url 
+1

Hai confrontato il tuo codice con [implementazione di riferimento] (https://github.com/railstutorial/sample_app_rails_4)? Il codice che hai elencato sembra funzionare correttamente sul mio sistema. – mhartl

+0

per me anche questa risposta e la risposta di @AmanGarg perché avevo cambiato l'url da localhost: 3000 nel mio ambiente di sviluppo e non ho mai fatto la stessa modifica nel mio test env –

+0

avuto lo stesso errore. non ha cambiato nulla. appena fermato il server e riavviato di nuovo. e ha funzionato. – Emanuel

61

Il problema potrebbe essere che non è stato definito default_host per l'ambiente di test. Definire default_host all'interno config/ambienti/test.rb come questo:

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

Sempre con lo stesso errore. Questo non sembra fare alcuna differenza. – Sheldon

+3

Ricordarsi di riavviare il server delle guide dopo aver modificato le configurazioni. –

+2

Aggiungi 'Rails.application.routes.default_url_options [: host] = 'localhost: 3000'' alla fine di 'test.rb'. (Dopo il blocco 'do ... end') –

4

Hai per impostare il default_url in ogni ambiente (sviluppo, test, produzione).

È necessario apportare queste modifiche.

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

È possibile evitare questo errore utilizzando il skip_confirmation! Metodo.

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

Per test come test controllore mostrate nell'esempio del PO, è necessaria un'altra impostazione per risolvere questo errore. Menzionato da Kesha Antonov puoi invece utilizzare le opzioni predefinite_url_opzioni di Routes. Aggiungendo la seguente impostazione sull'ultima riga del tuo config/ambienti/test.rb (dopo la fine ), i test utilizzeranno l'host data per costruire URL:

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

Come accennato dal PO in un'altra risposta, è possibile invece passare a un percorsoNamed Route quando l'errore proviene da un URL denominato percorso.

Se si desidera impostare l'host predefinito per le anteprime dei mailer, si desidera che action_mailer.default_url_options sia menzionato in altre risposte.

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

vedere Generazione di URL nei Action Mailer API documentazione per ulteriori informazioni sull'impostazione action_mailer.

Problemi correlati