M. Hartl Rails 튜토리얼을 사용하여 내 앱을 만들었습니다. 따라서 User
모델이 있고 모두 current_user
및 signed_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)
내에 있어야합니다.
'unless! current_user.nil?'이것은이 경우 쓸 수있는 최악의 논리입니다. 'current_user.nil? '또는'current_user.present가 아닌가?'를 의미하는 것입니까? 그럼에도 불구하고, 나는 패치 요청이 당신의 어플리케이션에 세션 쿠키를 보내지 않는다고 가정하므로,'current_user'는 없습니다. – phoet
그리고 세션 쿠키를 어떻게 시뮬레이트 할 수 있습니까? –