使用raise_error进行RSpec授权测试不起作用

时间:2013-05-05 21:39:22

标签: ruby-on-rails rspec cancan rspec-rails

我正在尝试测试未登录用户的行为如何

  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.

所以看起来提出了正确的execption,但为什么规范没有通过?

1 个答案:

答案 0 :(得分:19)

我已将规格更改为:

 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

它有效。感谢托马斯! : - )