2013-09-30 3 views
0

M. Hartl Rails 튜토리얼을 사용하여 내 앱을 만들었습니다. 따라서 User 모델이 있고 모두 current_usersigned_in_user 메소드가 있습니다.로그인 한 사용자가 로그인 할 수없는 것 같습니다.

나는 다음 테스트 통과 만들고 싶어 :

describe "submitting a PATCH request to the Users#update action" do 
    before do 
    be_signed_in_as FactoryGirl.create(:user) 
    patch user_path(FactoryGirl.create(:user)) 
    end 
    specify { expect(response).to redirect_to(root_path) } 
end 

을하지만 테스트가 실패 : 그래서 여기

Failure/Error: specify { expect(response).to redirect_to(root_path) } 
    Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/signin>. 
    Expected "http://www.example.com/" to be === "http://www.example.com/signin". 

사용자 컨트롤러

class UsersController < ApplicationController 

    before_action :signed_in_user, only: [:index, :edit, :update, :destroy] 
    before_action :correct_user, only: [:edit, :update] 
    before_action :admin_user, only: :destroy 

     . 
     . 
     . 
     . 
    private 

    def signed_in_user 
     unless !current_user.nil? 
     store_url 
     redirect_to signin_url, notice: t('sign.in.please') 
     end 
    end 

    def correct_user 
     @user = User.find(params[:id]) 
     redirect_to(root_path) unless current_user?(@user) 
    end 

    def admin_user 
     redirect_to(root_path) unless current_user.admin? 
    end 
end 

의 일부입니다 before_create :signed_in_user... 행을 제거하면 테스트가 통과합니다. 하지만 그 이유는 무엇입니까? be_signed_in_as spec 메소드는 다른 모든 test (~ 1k)에서 작동하므로 이유는 specify { expect(response) 내에 있어야합니다.

+0

'unless! current_user.nil?'이것은이 경우 쓸 수있는 최악의 논리입니다. 'current_user.nil? '또는'current_user.present가 아닌가?'를 의미하는 것입니까? 그럼에도 불구하고, 나는 패치 요청이 당신의 어플리케이션에 세션 쿠키를 보내지 않는다고 가정하므로,'current_user'는 없습니다. – phoet

+0

그리고 세션 쿠키를 어떻게 시뮬레이트 할 수 있습니까? –

답변

0

correct_user 필터로 루트로 리다이렉트됩니다. 그러면 로그인하는 사용자가 아닌 다른 사용자의 테스트가 user_path이 될 것입니다. 로그인하는 사용자를 저장하고 user_path에 사용해야합니다.

0

FactoryGirl.create(:user)으로 전화 할 때마다 추가 사용자가 생성됩니다. 목록에있는 코드는 데이터베이스에 두 개의 개별 사용자 레코드를 만드는 것입니다. 그래서 당신은이 테스트를 위해 두 개의 서로 다른 사용자를 만들기에 의도하지 않는 한, 당신은 아마 전에 라인을 가져야한다 before 블록처럼 : 당신은 하나의 사용자 레코드를 원하는 모든 곳에서 그럼 그냥 user를 참조

let(:user) { FactoryGirl.create(:user) } 

.

관련 문제