首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试CanCanCan能力定义

测试CanCanCan能力定义
EN

Stack Overflow用户
提问于 2014-11-15 10:10:24
回答 1查看 1.4K关注 0票数 1

我正在使用CanCanCan和Rolify,我正在尝试测试我的能力,类授权。

在测试非特权用户是否可以在系统中CRUD其他用户时,测试失败

代码语言:javascript
复制
1) Ability a guest user should not be able to manage others
 Failure/Error: expect(subject).to_not be_able_to(:crud, User)
   expected not to be able to :crud User(...)

但是我找不到为什么我的能力类检查失败的任何原因:

代码语言:javascript
复制
class Ability
  include CanCan::Ability

  def initialize(user = User.new)
    alias_action :create, :read, :update, :destroy, :destroy_multiple, to: :crud

    # What is wrong?
    can :crud, User, id: user.id

    if user.has_role?(:admin)
      can :manage, User
    end
  end
end

这是我的规格:

代码语言:javascript
复制
require 'rails_helper'
require 'cancan/matchers'

RSpec.describe Ability do
  let(:user) { create(:user) }
  subject { Ability.new(user) }

  context "a guest user" do
    it "should be able to manage self" do
      expect(subject).to be_able_to(:crud, user)
    end

    it "should not be able to manage others" do
      expect(subject).to_not be_able_to(:crud, User)
    end
  end
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-15 15:37:40

代码语言:javascript
复制
expect(subject).to_not be_able_to(:crud, User) 

您所引用的是用户模型,而不是实例。使用User.new或其他持久化用户实例。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26944712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档