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

3

5

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)
Lindyline answered 10/9, 2014 at 11:53 Comment(0)
L
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 :)

Lindyline answered 10/9, 2014 at 15:19 Comment(0)
P
15

Also remember to add this your rails_helper

require 'pundit/rspec'
Pileum answered 9/3, 2015 at 11:35 Comment(1)
My tests were failing with this error when I started using parallel_tests. This require on rails_helper solved my problem.Elvinaelvira
L
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 :)

Lindyline answered 10/9, 2014 at 15:19 Comment(0)
D
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
Dafodil answered 15/8, 2021 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.