4

I'm installing an app with Pundit authorization and when I try to run RSpec tests I get:

undefined method `permissions'
  for RSpec::ExampleGroups::UserPolicy:Class
      (NoMethodError)
notapatch
  • 6,569
  • 6
  • 41
  • 45
gtheys
  • 501
  • 3
  • 19

3 Answers3

14

Also remember to add this your rails_helper

require 'pundit/rspec'
martins
  • 9,669
  • 11
  • 57
  • 85
  • My tests were failing with this error when I started using `parallel_tests`. This `require` on `rails_helper` solved my problem. – Thiago Borges Jun 30 '17 at 13:10
0

Found out what the problem was...

Following line in rails_helper.rb was commented out:

# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

Activating it made the tests work correctly :)

gtheys
  • 501
  • 3
  • 19
0

I had require 'pundit/rspec' but I had misspelt "policies" in the spec directory structure:

spec/polices/my_policy_spec.rb

My mistake also returned the permission error message.

You can fix it by spelling policies correctly, alternatively, set the type of the test file to policy.

RSpec.describe MyPolicy, type: :policy do
  ...
  ...
end
notapatch
  • 6,569
  • 6
  • 41
  • 45