rspec-railscancancan

how to write rspec for ability using cancancan


I have a implemented the post and user model where post model belongs to user model. I defined the ability model for authorisation so that only user who created the post can able to delete or update the post. i have the post controller like this:

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

Ability model:

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

What will be the rspec for the above model? Error:

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)>'


Solution

  • Based on the limited information you have provided, I'm going to share a sample spec which tests abilities.

    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
    

    What I have provided at the top was a Sample, using which you have to build upon it. My updated answer

    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