Search code examples
ruby-on-railsrspeccancanrspec-rails

RSpec authorization testing with raise_error not working


I'm trying to test how a not logged in user behaves like this

  describe "not logged in user" do
   user_no_rights

    it "can't access action index" do
      expect(get :index).to raise_error(CanCan::AccessDenied)
    end
  end

The output when i run rspec

  Failure/Error: expect(get :index).to raise_error("CanCan::AccessDenied:You are not authorized to access this page.")
     CanCan::AccessDenied:
       You are not authorized to access this page.

So it looks like the correct execption is raised, but why is the spec not passing?


Solution

  • I've changed my spec to:

     describe "not logged in user" do
       user_no_rights
    
        it "can't access action index" do
          expect{get :index}.to raise_error(CanCan::AccessDenied)
        end
     end
    

    and it works. Kudos to Thomas! :-)