2013-05-05 67 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 

和它的作品。榮譽托馬斯! :-)