Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 376
patrickkeller Avatar asked May 05 '13 21:05

patrickkeller


1 Answers

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! :-)

like image 170
patrickkeller Avatar answered Oct 19 '22 03:10

patrickkeller