NoMethodError:undefined method `id' for nil:NilClass

I'm new to Rspec and I am trying to do the TDD. In the Application Controller, I have a method called set current user.

class ApplicationController < ActionController::Base
   protect_from_forgery with: :exception
   protected  
    def set_current_user
      @current_user ||= User.find_by_session_token(cookies[:session_token]) 
      redirect_to login_path unless @current_user
    end
  end

and here is the BlogsController.rb

class BlogsController < ApplicationController
  before_action :set_current_user
  before_action :has_user_and_hobby
  def blog_params
    params.require(:blog).permit(:title, :hobby_id, :user_id, :body, :rating)
  end

 ...

  def destroy
    @blog = Blog.find(params[:id])
    if @blog.user_id != @current_user.id
      flash[:notice] = "The blog #{@blog.title} only can be deleted by the author! It cannot be deleted by others."
      redirect_to hobby_blogs_path(@blog)
    else
      @blog.destroy
      flash[:notice] = "Blog '#{@blog.title}' deleted."
      redirect_back(fallback_location: root_path)
    end
  end
end

And the rspec I wrote to test the destroy route is:

require 'spec_helper'
require 'rails_helper'

describe BlogsController do
    let(:fuser) { FactoryGirl.create(:fuser) }
    let(:hobby) { FactoryGirl.create(:hobby)}
    let(:blog) { FactoryGirl.create(:blog, hobby_id: hobby.id, user_id: fuser.id)}
    let(:comment) { FactoryGirl.create(:comment)}

...

    describe 'delete a blog' do
      before :each do
          allow_any_instance_of(ApplicationController).to receive(:set_current_user).and_return(fuser)
          allow_any_instance_of(BlogsController).to receive(:has_user_and_hobby).and_return(blog.user_id,hobby)
          allow(User).to receive(:find).with(blog.user_id).and_return(blog.user_id)

      it 'should redirect_back' do
          delete :destroy, params:{:hobby_id =>hobby.id, :id => blog.id}
          expect(response).to be_redirect
      end
   end
end

When I try to run the spec, I get the error:

Failure/Error: if @blog.user_id != @current_user.id
 NoMethodError:
   undefined method `id' for nil:NilClass

Does anyone know how to assist me in this? Greatly appreciate all the help.

1 answer

  • answered 2017-12-06 02:08 Daniel Westendorf

    @current_user is nil in your test.

    Your problem is here.

    allow_any_instance_of(ApplicationController).to receive(:set_current_user).and_return(fuser)
    

    set_current_user doesn't actually return a user object, it assigns one to a @current_user variable and then possibly redirects.

    It's much more the rails way do set your user in this manner:

    class ApplicationController < ActionController::Base
      before_action :verify_current_user!
    
      def current_user
        @current_user || User.find_by_session_token(cookies[:session_token])
      end
    
      def verify_current_user!
        redirect_to login_path unless current_user
      end
    end
    

    Then when referencing your currently signed in user, call the current_user method. The value will be memoized, so there is no performance penalty. You'll also be able to stub the current_user method as you are attempting in your test. In your controllers, always call current_user instead of @current_user.