Testing Rails Controller Helper Methods Using RSpec

If you are new to this. First read this article about Anonymous Controller. That should answer 98% of your questions. However, if you run in to problems the following tid bits of information might help ;)

Summary of Anonymous Controller

(code stolen from https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/controller-specs/anonymous-controller)

class ApplicationController < ActionController::Base  
    class AccessDenied < StandardError; end  
    rescue_from AccessDenied, :with => :access_deniedprivate  

    def access_denied    
        redirect_to "/401.html"  
    end
end

require "rails_helper"

RSpec.describe ApplicationController do  
    controller do    
        def index      
            raise ApplicationController::AccessDenied    
        end  
    end  

    describe "handling AccessDenied exceptions" do    
        it "redirects to the /401.html page" do      
            get :index      
            expect(response).to redirect_to("/401.html")    
        end  
    end
end  

Gotchas

Missing helper methods

If RSpec is having problems finding your helper methods within the Anonymous controller (it shouldn’t, because by default the controller should inherit from the controller you are testing), try the following.

require "rails_helper"

RSpec.describe ApplicationController do  
    controller(ApplicationController) do    
        def index      
            raise ApplicationController::AccessDenied    
        end  
    end
end  

Anonymous Controller Routes

RSpec magic only define resource routes by default. Meaning your usual index new create…etc actions will work without any issues. However, if you were to introduce a action outside of this, the routing wouldn’t be in place and you’d run in to problems. Example bellow.

Look at https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller#draw-custom-routes-for-anonymous-controllers for

require "rails_helper"

RSpec.describe ApplicationController do  
    controller do    
        def test_action      
            raise ApplicationController::AccessDenied    
        end  
    end  

    describe "handling AccessDenied exceptions" do    
        it "redirects to the /401.html page" do
            get :test_action      
            expect(response).to redirect_to("/401.html")    
        end  
   end
end  

References

Happy Testing!