vendredi 27 mars 2015

Why pundit can't find policies in controller tests?

The controller:



class UsersController < ApplicationController
def index
...
authorize User
end
...


The policy:



class UserPolicy < ApplicationPolicy
def index
@user.admin?
end
end


The test:



class UsersControllerAuthorizationTest < ActionController::TestCase
tests :users

def user
@user ||= create(:user)
end

test 'should not authorize ordinary users to access the page' do
sign_in user
get :index
assert_response :error
end
end


The app fails with Pundit::NotAuthorizedError (not allowed to index? this User) as expected. But the test says:



Pundit::NotDefinedError: unable to find policy UserPolicy for User


Am I doing it wrong? Can I make it find the policy?


Aucun commentaire:

Enregistrer un commentaire