2013-05-05 63 views
12

我想测试如何在不登录的用户RSpec的授权测试的行为像这样与raise_error不工作

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 

当我运行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. 

所以它看起来像输出正确的执行被提出,但为什么规范不通过?

+12

在提出错误'expect'的情况下,可能需要传递一个块:'expect {get:index} .to raise_error(CanCan :: AccessDenied)'。 – 2013-05-05 21:43:53

+0

谢谢@ThomasKlemm! – Zippie 2013-12-19 11:35:51

回答

18

我已经改变了我的规格来:

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 

和它的作品。荣誉托马斯! :-)