使用Devise JWT针对JSON控制器运行Rails RSPEC时,找不到有效的nil映射

时间:2018-10-28 22:01:28

标签: ruby-on-rails rspec devise jwt

尝试对具有Devise JWT身份验证的JSON控制器运行测试时获得runtime error: Could not find a valid mapping for nil。知道我错过了什么吗?

控制器

class Api::V1::TestController < ApplicationController
    before_action :authenticate_user!

    def index
        render json: {
            message: 'Sucessfully ran test API v1 endpoint.'
        },
        status: :ok
    end
end

测试

require 'rails_helper'
require 'devise/jwt/test_helpers'

RSpec.describe Api::V1::TestController, type: :controller do
  context "Test Controller" do
    it 'should return Forbidden with message from Test API' do
      headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
      auth_headers = Devise::JWT::TestHelpers.auth_headers(headers, nil)
      get :index, headers: auth_headers
      assert_response :unauthorized
    end
  end
end

错误

Api::V1::TestController Test Controller should return OK with message from Test API
     Failure/Error: auth_headers = Devise::JWT::TestHelpers.auth_headers(headers, user)

RuntimeError:
  Could not find a valid mapping for nil

0 个答案:

没有答案