在控制器级别测试未经授权的 cancan 动作是否有任何优势,或者这是多余的?

Are there any advantages to testing unauthorized-by-cancan actions at the controller level, or is this superfluous?

假设我定义了以下 CanCan 能力

can :index, Project
cannot :show, Project

和一个ability_spec

it { is_expected.to have_abilities([:index], Project }
it { is_expected.to not_have_abilities([:show], Project }

是否存在在控制器级别测试未经授权的操作是有利的,或者这是多余的?

describe "GET #show" do
  it "raises Access Denied" do 
    expect { 
      get :show, { id: subject.id} 
    }.to raise_error(CanCan::AccessDenied)
  end
end

我想说的是,只要有你的能力配置规范就可以了,因为这会锻炼你应用程序的业务逻辑。当能力未经授权执行操作时引发 CanCan::AccessDenied 错误的功能是 specced in the CanCan library itself.

如果您在访问被拒绝时遇到了自己的自定义错误 类,那么我认为可以编写您提到的那种控制器规范。