pundit: RSpec + Pundit; Undefined method all? and none?

This may be something really simple, but I was unable to find any answer after extensive googling.

I’m trying to run the following RSpec test:

require "spec_helper"

describe ReportPolicy do
  subject { ReportPolicy.new(user, report) }

  let(:user)    { FactoryGirl.create(:user) }
  let(:report)  { FactoryGirl.create(:report, organization_id: user.organization_id) }

  context "for a user" do
    it { should       permit(:show) }
    it { should_not   permit(:edit) }
  end  
end

It is failing with the errors:

1) ReportPolicy for a user should permit :show
     Failure/Error: it { should       permit(:show) }
     NoMethodError:
       undefined method `all?' for nil:NilClass
     # ./spec/policies/report_policy_spec.rb:10:in `block (3 levels) in <top (required)>'
2) ReportPolicy for a user should not permit :edit
     Failure/Error: it { should_not   permit(:edit) }
     NoMethodError:
       undefined method `none?' for nil:NilClass
     # ./spec/policies/report_policy_spec.rb:11:in `block (3 levels) in <top (required)>'

Can anyone explain what is going on here?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 17

Most upvoted comments

Pundit includes a mini-DSL for writing expressive tests for your policies in RSpec. Require pundit/rspec in your spec_helper.rb:

require “pundit/rspec” # Remove this line in your spec_helper.rb

You’re not using the matchers the right way. I admit they’re kinda weird. Essentially you need to wrap them in a permissions block, as demonstrated in the README. https://github.com/elabs/pundit#policy-specs