2013-03-17 3 views
0

컨트롤러를 테스트해야하는 rspec 테스트가 있습니다. facebook_profile.user_q1 유효한 결과를 얻을 수 있도록 @rspec 컨트롤러 테스트에서 변수 할당

it "renders the #show view" do 
    get :show, id: FactoryGirl.create(:quiz) 
    @facebook_profile = FactoryGirl.create(:facebook_profile) 
    response.should render_template :show 
end 

facebook_profile 객체는 user_q1 열을 갖는다.

@user_q1 = @facebook_profile.user_q1 

이 수동 테스트에서 잘 작동하지만 RSpec에, 나는이 결과를 얻을 : 내가 문제가 첫 번째 줄에, 여기에 의심

undefined method `user_q1' for nil:NilClass 

을 내 퀴즈 컨트롤러에서

@facebook_profile = FacebookProfile.find_by_facebook_id(session[:facebook_profile_id]) 

나는 내 세션 컨트롤러에 변수가있다. 어떤 모델의 칼럼) 내가 facebook_profile_id라고 부른다. 그런 다음 퀴즈 컨트롤러에서 위의 코드 줄에서이 변수를 호출합니다. 내 사양은 facebook_profile_id를 알지 못하기 때문에 @facebook_profile을 정의 할 수 없다고 생각합니다. 외관상으로는 "@facebook_profile = FactoryGirl"을 정의하는 것만으로는 작동하지 않습니다.

답변

1

당신은 이런 식으로 진행해야합니다 :

let(:fb_profile) { FactoryGirl.create(:facebook_profile) } 
let(:quiz) { FactoryGirl.create(:quiz)) 

it "renders the #show view" do 
    FacebookProfile.should_receive(:find_by_facebook_id).and_return fb_profile 
    get :show, id: quiz.id 
    response.should render_template :show 
end 

은 실제로 당신이 DB에서 개체를 만들 필요가 없습니다,하지만 그것은 또 다른 논쟁이다.

관련 문제