Home > database >  Test a policy with rspec
Test a policy with rspec

Time:10-21

I have this policy for cards, only i am using scope, its very simple, but once i tried to test with rspec i am getting errors, i am pasting my code and the below of that is the error:

this is the test, i am creating user, card and the scope, i dont know how can i solve that, for me everything is ok, help to realize with the error please

require 'rails_helper'

RSpec.describe CardPolicy, type: :policy do
  let(:card) {  FactoryBot.build_stubbed :card }
  let(:scope) { Pundit.policy_scope(user, Card.all) }
    
  describe "Scope" do
    context 'admin user' do
      let(:user) { User.new(role: 'admin') }
      it 'allows access to all the reports' do
        expect(scope).to match_array([card])
      end
    end
  end
end

this is the error:

Failure/Error: actual collection contained: []

i believe this is the error, because scope is empty:

let(:scope) { Pundit.policy_scope(user, Card.all) }

CodePudding user response:

In Pundit.policy_scope(user, card) you're passing an instance of a Card to policy_scope which is unlikely to respond to all

Try using multiple cards/items instead (e.g something that works like Card.all).

  • Related