Reputation: 123
I have an rspec test in which I need to test my controller.
it "renders the #show view" do
get :show, id: FactoryGirl.create(:quiz)
@facebook_profile = FactoryGirl.create(:facebook_profile)
response.should render_template :show
end
The facebook_profile object has a user_q1 column, so @facebook_profile.user_q1 gives a valid result.
In my quiz controller:
@user_q1 = @facebook_profile.user_q1
This works fine in manual testing, but in rspec, I get the result:
undefined method `user_q1' for nil:NilClass
I suspect the issue is here, in the first line of my show method in the controller:
@facebook_profile = FacebookProfile.find_by_facebook_id(session[:facebook_profile_id])
I have a variable in my session controller (although not a column in any model) that I call facebook_profile_id. I then call this variable in the above line of code in my quiz controller. I think my spec can't define @facebook_profile because it doesn't know facebook_profile_id. Apparently just defining "@facebook_profile = FactoryGirl" (like I have above) doesn't work.
Upvotes: 0
Views: 597
Reputation: 115511
You should proceed this way:
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
Actually you don't need to create objects in db, but that's another debate.
Upvotes: 1