FactoryGirl Rspec ActionView::Template::Error: undefined method for nil:NilClass-Record and share programming errors

I’m new to rails and am trying to give TDD a try.

I have a user model that has an admin attribute that is set to nil by default and a request model.

Here’s a test that I have for my request controller

it "should grant access to 'destroy'" do
        req = Factory(:request, :user => @user)
        delete :destroy, :id => req.id
        response.should be_successful
end

When I run this I get the following error:

ActionView::Template::Error:undefined method `admin' for nil:NilClass

I’m guessing this is because my views have links that only show up if the user owns the link or if they are an admin. So, I’m doing conditional testing on the admin attribute. Do I need to set the admin attribute to false?

How do I deal with this?

  1. I made a mistake. I tried to call user.admin in my controller when the user was nil. I created a helper method to check if user was nil before checking the admin field.

    def admin?(user)
      if not user.nil?
        return user.admin
      end
      return false
    end
    

Originally posted 2013-08-31 06:18:26.