Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 测试设计用户登录,但得到401个未经授权的响应_Ruby On Rails_Ruby_Devise_Minitest_Rails Api - Fatal编程技术网

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头中?