用户在rspec中登录后未经授权的请求

时间:2019-03-28 14:54:13

标签: ruby-on-rails rspec devise ruby-on-rails-5 rspec-rails

用户登录后,我收到了未经授权的请求(在:show上获取请求)。这是一个简单的用户测试示例,我看不到我在做什么错。

支持的控制器宏:

module ControllerMacros    
  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = FactoryBot.create(:user, company: FactoryBot.create(:company))
      sign_in user
    end
  end
end

Rspec助手:

require 'spec_helper'
require 'devise'
require 'support/controller_macros'

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
  config.include Devise::Test::ControllerHelpers, type: :controller
  config.extend ControllerMacros, type: :controller
end

用户测试:

require 'rails_helper'

RSpec.describe Api::V1::UsersController do
  include Devise::Test::ControllerHelpers

  describe "GET /api/v1/users" do
    login_user

    it "should get show" do
      get 'show'
      expect(response).to have_http_status(:success)
    end
  end
end

错误:

  

失败/错误:expect(response).to have_http_status(:success)          期望响应具有成功状态代码(2xx),但它是401

0 个答案:

没有答案