2013-08-25 58 views
15

tôi sau RoR Tutorial và tôi đang bị mắc kẹt tại Listing 9.15Làm cách nào để giải quyết "Thiếu máy chủ để liên kết tới! Vui lòng cung cấp thông số: host"? (ROR)

tôi nhận được lỗi sau sau khi chạy 'bó exec rspec đặc tả /':

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

My mã kiểm tra xác thực là:

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: "[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 

Tôi không biết cách giải quyết vấn đề này để bài kiểm tra trôi qua. Cách do Tôi giải quyết nó? Ai đó có thể giải thích những gì đang xảy ra? (Theo hướng dẫn bài kiểm tra nên được thông qua).

Trả lời

21

Cuối cùng tôi chỉ sử dụng:

root_path 

thay vì:

root_url 
+1

Bạn có so sánh mã của mình với [tham chiếu triển khai] (https://github.com/railstutorial/sample_app_rails_4) không? Mã bạn liệt kê xuất hiện để hoạt động OK trên hệ thống của tôi. – mhartl

+0

cho tôi nó quá câu trả lời này và câu trả lời bởi @ AmanGarg vì tôi đã thay đổi url từ localhost: 3000 trong môi trường dev của tôi và tôi không bao giờ thực hiện thay đổi tương tự trong bài kiểm tra của mình env –

+0

có lỗi tương tự. không thay đổi gì cả. vừa dừng máy chủ và khởi động lại nó. va no đa hoạt động. – Emanuel

61

Sự cố có thể do bạn chưa xác định default_host cho môi trường thử nghiệm. Xác định default_host bên config/môi trường/test.rb như thế này:

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

Vẫn gặp lỗi tương tự. Điều đó dường như không tạo ra bất kỳ sự khác biệt nào. – Sheldon

+3

Tâm trí để khởi động lại máy chủ đường ray sau khi thay đổi cấu hình. –

+2

Thêm 'Rails.application.routes.default_url_options [: host] = 'localhost: 3000'' ở cuối 'test.rb'. (Sau khối 'do ... end') –

4

Bạn đã thiết lập default_url trong mỗi môi trường (phát triển, thử nghiệm, sản xuất).

Bạn cần thực hiện những thay đổi này.

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

Bạn có thể tránh lỗi này bằng cách sử dụng số skip_confirmation!.

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

Đối với các thử nghiệm như kiểm tra bộ điều khiển được hiển thị trong ví dụ của OP, cần thiết lập khác để khắc phục lỗi này. Được đề cập bởi Kesha Antonov, bạn có thể sử dụng default_url_options của Tuyến đường. Bằng cách thêm vào các thiết lập sau trên dòng cuối cùng của cấu hình của bạn/môi trường/test.rb (sau khi cuối), kiểm tra của bạn sẽ sử dụng các máy chủ được xây dựng URL:

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

Như đã đề cập bởi OP trong một câu trả lời khác, thay vào đó bạn có thể chuyển sang Đường dẫn Named Route khi lỗi từ một URL có tên là tuyến đường.

Nếu bạn muốn đặt máy chủ mặc định cho xem trước người gửi thư, bạn sẽ muốn action_mailer.default_url_options được đề cập trong các câu trả lời khác.

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

URL Xem Generating trong Action Mailer API tài liệu để biết thêm thông tin về các thiết lập action_mailer.