Ruby on rails 测试设计用户登录,但得到401个未经授权的响应
我在Ruby on rails 测试设计用户登录,但得到401个未经授权的响应,ruby-on-rails,ruby,devise,minitest,rails-api,Ruby On Rails,Ruby,Devise,Minitest,Rails Api,我在rails-api应用程序中使用designe\u-auth\u-tokengem,试图测试用户是否登录然后用户被授权通过欢迎控制器中的索引操作呈现 欢迎\u控制器 class Api::WelcomeController < ApplicationController before_action :authenticate_user! def index render json: { data: { message: "Welcome #
rails-api
应用程序中使用designe\u-auth\u-token
gem,试图测试用户是否登录然后用户
被授权通过欢迎控制器中的索引操作呈现
欢迎\u控制器
class Api::WelcomeController < ApplicationController
before_action :authenticate_user!
def index
render json: {
data: {
message: "Welcome #{current_user.first_name}",
user: current_user
}
}, status: 200
end
end
Failure:
Api::WelcomeControllerTest#test_index_renders_message [/Users/ahmadhamza/sites/manage_society/test/controllers/api/welcome_controller_test.rb:8]:
Expected response to be a <2XX: success>, but was a <401: Unauthorized>
欢迎\u控制器\u测试
require 'test_helper'
class Api::WelcomeControllerTest < ActionDispatch::IntegrationTest
def test_index_renders_message
admin = users :admin
sign_in admin
get "/api/welcome"
assert_response :success
end
end
测试助手.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
class ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
end
错误
class Api::WelcomeController < ApplicationController
before_action :authenticate_user!
def index
render json: {
data: {
message: "Welcome #{current_user.first_name}",
user: current_user
}
}, status: 200
end
end
Failure:
Api::WelcomeControllerTest#test_index_renders_message [/Users/ahmadhamza/sites/manage_society/test/controllers/api/welcome_controller_test.rb:8]:
Expected response to be a <2XX: success>, but was a <401: Unauthorized>
故障:
Api::WelcomeControllerTest#test(测试)index(呈现)消息[/Users/ahmadhamza/sites/manage(管理)society/test/controllers/Api/welcome(欢迎)controller(测试.rb
预期响应为a,但为a
如何检查发生了什么错误?我认为您需要在get
请求中包含令牌。您需要这样做:get”/api/welcome?designe_token_auth=“,format:“json”
我同意@etagwerker的观点,但可能会将令牌放在auth头中?