gtheys
gtheys

Reputation: 521

Rspec spec failing -- undefined method `permissions' for RSpec::ExampleGroups::UserPolicy:Class (NoMethodError)

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)

Upvotes: 5

Views: 2293

Answers (3)

notapatch
notapatch

Reputation: 7173

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

Upvotes: 0

martins
martins

Reputation: 10009

Also remember to add this your rails_helper

require 'pundit/rspec'

Upvotes: 15

gtheys
gtheys

Reputation: 521

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

Upvotes: 0

Related Questions