0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.