Pundit::NotDefinedError: unable to find policy `UserPolicy`
<p>I must be doing something wrong or I need glasses. I am following this tutorial here:</p>

<p><a href="http://vaidehijoshi.github.io/blog/2015/09/29/using-pundit-the-cool-kid-of-authorization/" rel="nofollow">http://vaidehijoshi.github.io/blog/2015/09/29/using-pundit-the-cool-kid-of-authorization/</a></p>

<p>I've created the <code>application_policy.rb</code> file and <code>user_policy.rb</code> files inside the <code>app/policies</code> folder as instructed.</p>

<p>Pundit doesn't seem to detect my UserPolicy file inside <code>IntegrationTests</code>.</p>

<h2>My Setup</h2>

<ul>
<li>Ruby on Rails 5 RC1</li>
<li>Ruby 2.2.4p230</li>
<li>Knock gem for JWT authentication</li>
<li>Pundit 1.1.0</li>
</ul>

<h2>user_policy.rb</h2>

<pre><code>class UserPolicy &lt; ApplicationPolicy    
  def update?
    user == resource
  end
end
</code></pre>

<p>In my UserController, I have the defined REST API actions. Currently, I'm just testing the "update" action:</p>

<h2>UsersController</h2>

<pre><code>def update
  user = User.find_by({id: params[:id]})

  authorize user

  render json: { error: "Failed to find" }, status: :not_found and return unless user

  if user.update!(user_params)
    render json: user
  else
    render json: { error: "Not found" }, status: :not_found
  end
end
</code></pre>

<h2>users_controller_test.rb</h2>

<pre><code>test "update other user's data should raise NotAuthorized exception" do
  @user_two = users(:two)

  put user_path(@user_two.id), params: { first_name: "Jim" }, headers: @authorization_header
  assert_response :success # forcing fail test for now to test plumbing
end
</code></pre>

<p>I am getting the following errors:</p>

<pre><code>.E

Error:
UsersControllerTest#test_update_other_user's_data_should_raise_NotAuthorized_exception:
Pundit::NotDefinedError: unable to find policy `UserPolicy` for `#&lt;User id: 298486374, first_name: "MyString", last_name: "MyString", email: "MyString", password_digest: "MyString", created_at: "2016-05-27 13:30:07", updated_at: "2016-05-27 13:30:07", role_id: nil&gt;`
    app/controllers/users_controller.rb:42:in `update'
    test/controllers/users_controller_test.rb:60:in `block in &lt;class:UsersControllerTest&gt;'
</code></pre>

<p>Any ideas what I might be doing wrong?</p>

<h2>Edit</h2>

<p>If it's any help, my UserControllerTest file looks like this:</p>

<pre><code>require 'test_helper'

class UsersControllerTest &lt; ActionDispatch::IntegrationTest
  def authenticate
    token = Knock::AuthToken.new(payload: { sub: users(:one).id }).token
    @authorization_header = { HTTP_AUTHORIZATION: "Bearer #{token}" }
  end

  setup do
    # load "#{Rails.root}/db/seeds.rb"
    Rails.application.load_seed
    authenticate
    @user = users(:one)
  end

  test "logged in user should return ok" do
    get users_path, headers: @authorization_header
    assert_response :ok
  end

  test "not logged in user should return unauthorized" do
    get users_path
    assert_response :unauthorized
  end

  test "user json should not contain password_digest" do
    get user_path(@user.id), headers: @authorization_header
    assert_response :ok
    json = JSON.parse(response.body)
    assert json.key?("password_digest") == false
  end

  test "create user without authorization header should return created" do
    user = {
      first_name: "Bob",
      last_name: "Brown",
      email: "bob@gmail.com",
      password: "abc",
      password_confirmation: "abc"
    }

    post users_path, params: user
    assert_response :created
    json = JSON.parse(response.body)
    assert !json.empty?
  end

  test "update user should return ok" do
    put user_path(@user.id), params: { first_name: "Bob"}, headers: @authorization_header
    assert_response :ok

    updated_user = User.find_by({id: @user.id})

    assert_equal "Bob", updated_user.first_name
  end

  test "update other user's data should raise NotAuthorized exception" do
    @user_two = users(:two)

    put user_path(@user_two.id), params: { first_name: "Jim" }, headers: @authorization_header
    assert_response :success
  end

  test "delete user shoudl return ok" do
    assert_difference "User.count", -1 do
      delete user_path(@user.id), headers: @authorization_header
    end
  end
end
</code></pre>

<p>All my tests were passing before I added the Pundit stuff an hour ago.</p>