我正在根据本指南测试康康舞能力:link
这是我的能力档案: 更新: 我已发布所有能力档案。
#models/ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new(role:Role.find_by(name:'guest')) # in case of guest
if user.has_role? :admin
can :manage, :all
end
if user.has_role? :author
can :read, :all
can :create, Msg
can :create, Post
can :destroy, Msg, user_id:user.id
can :destroy, Post, user_id:user.id
can :update, Post, user_id:user.id
can :update, Msg, user_id:user.id
can :destroy, Msg, post: { user_id:user.id }
end
if user.has_role? :moderator
can :read, :all
can :create, Msg
can :destroy, Msg
can :update, Msg , user_id:user.id
end
if user.has_role? :guest
can :read, Msg
can :read, Post
end
if user.has_role? :user
can :read, :all
can :create, Msg
can :destroy, Msg , user_id:user.id
can :update, Msg , user_id:user.id
end
end
这是我的规范测试:
require "cancan/matchers"
require 'spec_helper'
describe "when is moderator" do
before do
@u=FactoryGirl.create(:moderator)
@p=FactoryGirl.create(:post,user_id: 1245)#To be sure @p don't belongs to @u
@a=Ability.new(@u)
end
it{ @a.should be_able_to(:update, Post) } #Ok, it must be pass
it{ @a.should be_able_to(:update, FactoryGirl.create(:post,:user_id => @u.id)) } #Ok, it must be pass
it{ @a.should be_able_to(:update,@p) } #passed, ***but should fail***
end
我不知道要解决,在我看来这种行为是无法解释的