lundi 30 novembre 2015

Rspec: testing cancan ability with arguments

I am using cancan gem.

I have ability defined in my class:

module Gexcore
   class Myability
       include CanCan::Ability

    def initialize(user)
       can :delete_user, User do |victim|
      ..
        end
    end

  end
end

I am testing the ability to delete user with RSpec:

RSpec.describe "Permissions to delete user", :type => :request do
  describe 'del user' do
    it 'superadmin can delete user' do
      superadmin = double(User, :id => 10, <<some other fields>>)

      ability = Mymodule::Myability.new(superadmin)

      victim = double(User)

      res = superadmin_ability.can?(:user_del, victim)

      expect(res).to eq true

    end
  end

end

My user model: class User < ActiveRecord::Base delegate :can?, :cannot?, :to => :ability ... end

But it doesn't see method can? :delete_user, User from my ability. It expects User class but RSpec calls it with RSpec::Mocks::Double class.

How to test correctly ability from cancan gem using Rspec mocks ?

Aucun commentaire:

Enregistrer un commentaire