2016-06-27 3 views
0

컨트롤러 만들기 사양에서 ID가 누락되었습니다. 그러나 나는 매번 똑같은 문제에 직면 해있는 ID 물건을 통과 시켰습니다. 여기 사양 및 컨트롤러 파일을 생성의 나Rspec | ID가없는 컨트롤러 만들기

만들기 사양

describe 'POST :create' do 
    context 'with valid data' do 
     let(:valid_data) { FactoryGirl.attributes_for(:student) } 
     it 'redirect to show page' do 
     post :create, student: valid_data 
     expect(response).to redirect_to(student_path(assigns[:student])) 
     end 
    end 
    end 

학생 컨트롤러

def create 
    @student = current_user.students.build(student_params) 

    respond_to do |format| 
     if @student.save 
     format.html { redirect_to @student } 
     format.json { render :show, status: :created, location: @student } 
     else 
     format.html { render :new } 
     format.json { render json: @student.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

private 

    def student_params 
    params.require(:student).permit(:Student_Prefix, :First_Name, :Middle_Name, :Last_Name, :Father_Prefix, :Father_Name, :Father_Middle_Name, :Father_Last_Name, :Mother_Prefix, :Mother_Name, :Mother_Middle_Name, :Mother_Last_Name, :user_id) 
    end 

오류

1) StudentsController POST :create with valid data redirect to show page 
    Failure/Error: expect(response).to redirect_to(student_path(assigns[:student])) 

    ActionController::UrlGenerationError: 
    No route matches {:action=>"show", :controller=>"students", :id=>nil} missing required keys: [:id] 
    # ./spec/controllers/students_controller_spec.rb:38:in `block (4 levels) in <top (required)>' 
    # -e:1:in `<main>' 

답변

0

saveStudentsController이 아니므로 @student에는 ID가 없습니다.

expect(response).to redirect_to(student_path(assigns[:student])) 

assigns[:student]

는 ID가없는 : 당신이 당신의 사양에 경로를 구축하려고하면 오류가 발생하고 있습니다.

+0

ID를 컨트롤러에 전달할 때마다 동일한 오류가 발생했지만 root_path로 리다이렉트 경로를 변경해도이 오류가 발생했습니다. 오류/오류 : 예상 (응답) .to redirect_to (root_path) 예상 응답은 , 하지만 <200>' –

+0

컨트롤러는'@ student.save'가 실패하고 false를 반환하기 때문에 리디렉션을 보내지 않습니다. 테스트 파일의 오류를 살펴 보자 :'puts assigns [: student] .errors'. 이렇게하면'save'가 실패한 이유를 알 수 있습니다. –

+0

도움을 주셔서 감사합니다. 간단한'errors' 메소드 대신'errors.full_messages'를 사용하여 완전한 에러 메시지를 출력합니다. –

관련 문제