RSpec authorization testing with raise_error not working
Asked Answered
A

1

14

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?

Appressed answered 5/5, 2013 at 21:39 Comment(1)
In the case of raising an error expect might need to be passed a block: expect{ get :index }.to raise_error(CanCan::AccessDenied).Illimani
A
23

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

Appressed answered 6/5, 2013 at 22:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.