2013-08-04 16 views
6

Tôi đang cố gắng viết các kiểm tra và mã ứng dụng để chuyển hướng người dùng đã đăng nhập vào root_path nếu họ cố gắng TẠO người dùng hoặc truy cập đường dẫn người dùng MỚI .Hướng dẫn đường ray Ch. 9 Bài tập 6: Phản hồi dự kiến ​​là <redirect>, nhưng là <200>

Dưới đây là các bài kiểm tra tôi đã viết trong user_pages_spec.rb:

describe "for signed in users" do 
     let(:user) { FactoryGirl.create(:user) } 
     before { sign_in user } 

     describe "using a 'new' action" do 
     before { get new_user_path } 
     specify { response.should redirect_to(root_path) } 
     end 

     describe "using a 'create' action" do 
     before { post users_path } 
     specify { response.should redirect_to(root_path) } 
     end   
    end 

UsersController:

class UsersController < ApplicationController 
    before_action :unsigned_in_user, only: [:create, :new] 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(user_params) 
    if @user.save 
     sign_in @user 
      flash[:success] = "Welcome to the Sample App!" 
      redirect_to @user 
    else 
      render 'new' 
    end 
    end 

    private 
    # Before filters 

    def user_params 
     params.require(:user).permit(:name, :email, :password, 
           :password_confirmation) 
    end 

    def unsigned_in_user 
     puts signed_in? 
     redirect_to root_url, notice: "You are already signed in." unless !signed_in? 
    end 
end 

Các puts signed_in? lợi nhuận sai. Tôi giả định đây là vấn đề bởi vì tôi mong đợi nó sẽ trở lại sự thật. Dưới đây là các lỗi sau khi chạy thử nghiệm bằng rspec. Bất kỳ trợ giúp được đánh giá cao.

Failures: 

    1) User pages for signed in users using a 'create' action 
    Failure/Error: before { post users_path } 
    ActionController::ParameterMissing: 
     param not found: user 
    # ./app/controllers/users_controller.rb:52:in `user_params' 
    # ./app/controllers/users_controller.rb:20:in `create' 
    # ./spec/requests/user_pages_spec.rb:162:in `block (4 levels) in <top (required)>' 

    2) User pages for signed in users using a 'new' action 
    Failure/Error: specify { response.should redirect_to(root_path) } 
     Expected response to be a <redirect>, but was <200> 
    # ./spec/requests/user_pages_spec.rb:158:in `block (4 levels) in <top (required)>' 

Trong tập tin sessions_helper.rb:

def signed_in? 
    !current_user.nil? 
end 

Trong đặc tả/support/utilities.rb:

def sign_in(user, options={}) 
    if options[:no_capybara] 
    # Sign in when not using Capybara. 
    remember_token = User.new_remember_token 
    cookies[:remember_token] = remember_token 
    user.update_attribute(:remember_token, User.encrypt(remember_token)) 
    else 
    visit signin_path 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in" 
    end 
end 
+0

Đăng mã cho signed_in? phương pháp. Vì nó đang trả lại một cái gì đó bạn không mong đợi, vấn đề có lẽ nằm ở đó. Đã thêm – amb110395

+0

. Tôi mới đến đường ray nhưng tôi tự hỏi nếu thực tế là signed_in? được định nghĩa trong session_helper.rb và tôi đang cố gắng sử dụng nó trong bộ điều khiển người dùng (và không phải là bộ điều khiển phiên) đặt ra một vấn đề .. – IkegawaTaro

+0

Chắc chắn sẽ gây ra vấn đề trừ khi bạn đưa SessionHelper vào trong ApplicationController của mình. Bạn đã làm điểu đó? – amb110395

Trả lời

4

Were bạn có thể nhận được các bài kiểm tra của bạn để vượt qua?

Trong trường hợp bạn không gặp phải vấn đề tương tự như bạn ngày hôm nay và có thể thực hiện các thử nghiệm để vượt qua bằng cách thực hiện hai thay đổi đối với thử nghiệm - chuyển một mã băm user khi POST và sử dụng tùy chọn no_capybara phương pháp sign_in, kể từ getpost không phải là phương pháp capybara và tôi nghĩ rằng RSpec không hoạt động như chúng ta có thể mong đợi nếu chúng ta chuyển từ phương thức capybara sang không capybara trong cùng một thử nghiệm.

describe "for signed-in users" do 
    let(:user) { FactoryGirl.create(:user) } 
    before { sign_in user, no_capybara: true } 

    describe "using a 'new' action" do 
    before { get new_user_path }  
    specify { response.should redirect_to(root_path) } 
    end 

    describe "using a 'create' action" do 
    before do 
     @user_new = {name: "Example User", 
        email: "[email protected]", 
        password: "foobar", 
        password_confirmation: "foobar"} 
     post users_path, user: @user_new 
    end 

    specify { response.should redirect_to(root_path) } 
    end 
end 
+1

Rất tuyệt! Tôi đã thêm tùy chọn no_capybara: true vào bài kiểm tra đầu tiên và bây giờ cả hai đều vượt qua. Tôi đã không vượt qua một băm người dùng để tạo nhưng nó bây giờ vượt qua. Tôi đoán đó không phải là một yêu cầu ... tức làtừ các thử nghiệm micropost: mô tả "gửi tác vụ tạo", hãy thực hiện trước {post microposts_path} chỉ định {expect (response) .to redirect_to (signin_path)} end – IkegawaTaro

+1

Ồ, điều đó có ý nghĩa, bởi vì băm người dùng sẽ chỉ cần thiết nếu Rails thực sự thực hiện phương pháp tạo, đó là những gì chúng tôi đang cố gắng ngăn chặn. Cảm ơn - chúng tôi đều đang lấp đầy khoảng trống của nhau :) – najwa

3

Cùng câu trả lời như Najwa, nhưng tôi đã sử dụng FactoryGirl dùng với Rails thuộc tính phương pháp để tránh trùng lặp:

describe "using a 'create' action" do 
    before { post users_path, user: user.attributes } 

    specify { response.should redirect_to(root_path) } 
end 

Giúp giữ cho dữ liệu tách rời khỏi mã thử nghiệm.

Các vấn đề liên quan