我正在使用CanCanCan和Rolify,我正在尝试测试我的能力,类授权。
在测试非特权用户是否可以在系统中CRUD其他用户时,测试失败
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(...)但是我找不到为什么我的能力类检查失败的任何原因:
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这是我的规格:
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发布于 2014-11-15 15:37:40
expect(subject).to_not be_able_to(:crud, User) 您所引用的是用户模型,而不是实例。使用User.new或其他持久化用户实例。
https://stackoverflow.com/questions/26944712
复制相似问题