如何使用 cancancan 写 rspec 的能力

how to write rspec for ability using cancancan

我实现了 post 和用户模型,其中 post 模型属于用户模型。我定义了授权的能力模型,这样只有创建 post 的用户才能删除或更新 post。我有这样的 post 控制器:

def edit
  @post = @topic.posts.find(params[:id])
  authorize! :update, @post
end

能力模型:

class Ability
  include CanCan::Ability

  def initialize(user)
    can :update, Post do |p|
      p.user == user
    end

    can :destroy, Post do |p|
      p.user == user
    end

    can :destroy, Comment do |c|
      c.user == user
    end

    can :create, Post
    can :create, Comment
  end
end

上述模型的 rspec 是什么? 错误:

expected #<User id: nil, email: "", created_at: nil, updated_at: nil> to respond to `able_to?`

  0) User Check for Abilities What User can do should be able to :create and #<Post id: nil, title: nil, body: nil, created_at: nil, updated_at: nil, topic_id: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, user_id: nil>
     Failure/Error: it { should be_able_to(:create, Post.new) }
       expected #<User id: nil, email: "", created_at: nil, updated_at: nil> to respond to `able_to?`
     # ./spec/models/ability_spec.rb:8:in `block (4 levels) in <top (required)>'

expected #<User id: nil, email: "", created_at: nil, updated_at: nil> to respond to `able_to?`

  0) User Check for Abilities What User can do should be able to :update and #<Post id: nil, title: nil, body: nil, created_at: nil, updated_at: nil, topic_id: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, user_id: nil>
     Failure/Error: it { should be_able_to(:update, Post.new) }
       expected #<User id: nil, email: "", created_at: nil, updated_at: nil> to respond to `able_to?`
     # ./spec/models/ability_spec.rb:9:in `block (4 levels) in <top (required)>'

根据您提供的有限信息,我将分享一个测试能力的示例spec

describe "User" do
    describe "Check for Abilities" do
        let(:user) { FactoryGirl.create(:user) }

        describe "What User can do" do
            it { should be_able_to(:create, Post.new) }
            it { should be_able_to(:update, Post.new) }
        end
    end
end

我在顶部提供的是一个示例,您必须使用它来构建它。我更新的答案

require "cancan/matchers"

describe "User" do
  describe "abilities" do
    user = User.create!
    ability = Ability.new(user)
    expect(ability).to be_able_to(:create, Post.new)
    expect(ability).to_not be_able_to(:destroy, Post.new)
  end
end

让我们先稍微重构一下您的能力文件。使用 hash of conditions 而不是块总是一个好主意。因此,您的能力文件应如下所示:

class Ability
  include CanCan::Ability

  def initialize(user)
    can [:update, :destroy], Post, user_id: user.id
    can :destroy, Comment, user_id: user.id
    can :create, Post
    can :create, Comment
  end
end

这是您拥有的版本的较短版本,而且功能更多。

这是一个关于我如何编写测试的示例,基于 cancancan documentation:

require 'cancan/matchers'
RSpec.describe Ability do  
  subject(:ability) { described_class.new(user) }

  context 'when there is no user' do
    let(:user){ nil }

    it { is_expected.to_not be_able_to(:create, Post)
    ....
  end

  context 'when the user is present' do
    let(:user){ User.new }

    it { is_expected.to be_able_to(:create, Post) }
    ...
end

结束 结束

我把剩下的测试交给你。