Looking for some help, Im a noob to Rspec and pundit, I was following an example to setup pundit and have the testing of it, but all the test are failing with
NameError:
uninitialized constant UserPolicy
# ./spec/policies/user_policy_spec.rb:1:in `<top (required)>'
No examples found.
Need some help to figure out why as I have no idea :(
tried stackoverflow - https://stackoverflow.com/questions/51018559/pundit-rspec-uninitialized-constant-userpolicy-why
Using:
module PunditHelper
extend ActiveSupport::Concern
included do
include Pundit
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
end
private
def user_not_authorized
flash[:alert] = "Access denied."
redirect_to (request.referrer || root_path)
end
end
ApplicationController.send :include, PunditHelper
app/policies/user_policy.rb
class UserPolicy
include ApplicationHelper
attr_reader :current_user, :model
def initialize(current_user, model)
@current_user = current_user
@user = model
end
def index?
is_admin?(@current_user)
end
def show?
is_admin?(@current_user) or @current_user == @user
end
def update?
is_admin?(@current_user) or @current_user == @user
end
def destroy?
is_admin?(@current_user) or @current_user == @user
end
end
spec/support/pundit.rb
require 'pundit/rspec'
spec/rails_helper.rb
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if
Rails.env.production?
require 'rspec/rails'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
...
spec/policies/user_policy_spec.rb
describe UserPolicy do
subject { UserPolicy }
let (:current_user) { FactoryBot.build_stubbed :user }
let (:other_user) { FactoryBot.build_stubbed :user }
let (:admin) { FactoryBot.build_stubbed :user, :admin }
permissions :index? do
it "denies access if not an admin" do
expect(user_policy).not_to permit(current_user)
end
it "allows access for an admin" do
expect(user_policy).to permit(admin)
end
end
end
app/controllers/users_controller.rb
class UsersController < ApplicationController
include Pundit
before_action :authenticate_user!
after_action :verify_authorized
def index
@users = User.all
authorize User
end
....
end
Does it only fail in the test? Does it find the policy when you authorize in the controller?
yes only in the test. At that point, I thought it might have been guard or rails server so I have restarted both many times and still nothing
I was just having the same issue on a relatively new fresh project.
Rails 5.2
I've had to add require: true to the gem line in the Gemfile to solve it.
Can one of you create a minimal example where this happens it would help a lot.
So, I've now removed the required: false in the gem file, and it continues to work.
Also tried to replicate on a fresh Rails 5.2 test codebase, and, again no issue there.
On my side, I have no idea how to reproduce again :(
Ok, so let’s close this and if anyone can reproduce the issue on a minimal app we can reopen it.
Just wanted to chime in and say that I had this same problem: everything was working in the controller, the policies were visible from the rails console, but from rspec I kept getting the _uninitialized constant_ error. Could not figure it out.
Found this thread. Added require: true and the problem disappeared. I know this doesn't help you fix it! But wanted @nicalpi know they were not alone.
@thornomad can you create a small example app that has this issue please?
Sorry - unable to reproduce from a minimal setup (like @nicalpi). Not helpful, I know! Thanks for your work on this.
A little late – but just ran into this issue with a new project. I just had to stop spring manually and then everything worked: spring stop.
Todays tip:
export DISABLE_SPRING=true
Most helpful comment
A little late – but just ran into this issue with a new project. I just had to stop spring manually and then everything worked:
spring stop.