ruby-on-rails – RSPec授权测试,raise_error不工作

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 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.

所以看起来正确的提示,但为什么规范不通过?

解决方法

我已将我的规格改为:
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

它的工作原理托马斯! 原文链接:https://www.f2er.com/ruby/271845.html

猜你在找的Ruby相关文章