2
votes

I'm trying to test the POST create action within my polymorphic comments controller. When running the spec below they fail with the error:

undefined method `comments' for nil:NilClass

Which I think means that @commentable isn't being created/set up properly, so it doesn't exist. ATM I am stubbing out the load_commentable method and returning the FactoryGirl question object, however this still doesn't seem to solve anything.

How can I amend my spec so that the commentable object is created properly and the comment is created within the scope of @commentable, as in the actual controller?

comments_controller.rb

def create 
    @comment = @commentable.comments.build(params[:comment])
    @comment.user_id = current_user.id
    respond_to do |format|
    if @comment.save
        format.html { redirect_to @commentable, notice: 'Comment created'}
        format.js
    else
      format.html { redirect_to @commentable, notice: "Content can't be blank" }
      format.js
      end
    end
  end

def load_commentable
    resource, id = request.path.split('/')[1,2]
    @commentable = resource.singularize.classify.constantize.find(id)
  end

comments_controller_spec.rb

describe CommentsController do
  include Devise::TestHelpers
  include AnswerHelper
  before(:each) do
    @user = create(:user)
    @user2 = create(:user)
    sign_in @user
    sign_in @user2
    @commentable = create(:question, user: @user2)
    @comment = create(:comment, user: @user)
    @vote = attributes_for(:vote, user: @user2, votable_id: @commentable)
    controller.stub!(:load_commentable).and_return(@commentable)
    controller.stub!(:current_user).and_return(@user)
    @request.env['HTTP_REFERER'] = "http://test.host/questions/#{@commentable.id}"
    stub_model_methods
  end

  describe "POST create" do
    describe "with valid params" do
      it "creates a new comment" do
        expect {
          post :create, comment: attributes_for(:comment), commentable: @commentable
        }.to change(Comment, :count).by(1)
      end

      it "assigns a newly created comment as @comment" do
        post :create, comment: attributes_for(:comment), commentable: @commentable
        assigns(:comment).should be_a(Comment)
        assigns(:comment).should be_persisted
      end
    end

    describe "with invalid params" do
      it "assigns a newly created but unsaved comment as @comment" do
        Comment.any_instance.stub(:save).and_return(false)
        post :create, comment: attributes_for(:comment), commentable: @commentable
        assigns(:comment).should be_a_new(Comment)
      end
    end
  end

factory.rb

factory :comment do
    user
    commentable_id :question
    commentable_type "Question"
    content "a comment"
    votes_count 5
  end

rspec resullts

1) CommentsController POST create with valid params creates a new comment
     Failure/Error: post :create, comment: attributes_for(:comment), commentable: @commentable
     NoMethodError:
       undefined method `comments' for nil:NilClass
     # ./app/controllers/comments_controller.rb:19:in `create'
     # ./spec/controllers/comments_controller_spec.rb:24:in `block (5 levels) in <top (required)>'
     # ./spec/controllers/comments_controller_spec.rb:23:in `block (4 levels) in <top (required)>'
1

1 Answers

1
votes

The problem here is that you're stubbing out load_commentable, which is what is responsible for setting the instance variable @commentable in your controller. Since you're stubbing it, it never gets called, and the ivar never gets set - you can't set ivars in your controller from your rspec test suite directly.

Since you're creating a record, you don't actually need to stub anything, and can just pass the @commentable.id, then let it be looked up from the database. If you want to avoid the find for some reason, though, you could use:

Question.stub(:find).with(@commentable.id).and_return(@commentable)

This will cause your controller to use your @commentable object, and assign it to @commentable in the controller, at which point the test should continue to run normally.