为何我的RSpec无法加载Devise :: Test :: ControllerHelpers?

我正在使用Rails 5和Devise 3.5.1。

翻阅一本不错的(较旧的)有关创建/测试API的书,该书使用Devise身份验证。 它是在Rails 5之前编写的,因此我选择不使用新的仅API版本。

这是我的测试...

#/spec/controllers/api/v1/users_controller_spec.rb    

require 'rails_helper'

describe Api::V1::UsersController, :type => :controller do
    before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" }
    describe "GET #show" do
        before(:each) do
            @user = FactoryGirl.create :user
            get :show, params: {id: @user.id}, format: :json
        end
        it "returns the information about a reporter on a hash" do
            user_response = JSON.parse(response.body, symbolize_names: true)
            expect(user_response[:email]).to eql @user.email
        end
        it { should respond_with 200 }
    end
end

这是一个完全意外的RSpec错误

Devise::MissingWarden:
       Devise could not find the `Warden::Proxy` instance on your request environment.
       Make sure that your application is loading Devise and Warden as expected and that the `Warden::Manager` middleware is present in your middleware stack.
       If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the `Devise::Test::ControllerHelpers` module to inject the `request.env['warden']` object for you.

所以我去这里-[http://www.rubydoc.info/gems/devise/Devise/Test/ControllerHelpers]

并尝试了此->包括Devise :: Test :: ControllerHelpers

这没有帮助,因为文件controller_helpers.rb在我的项目中不存在

我在这里想念什么?

谢谢

dwilbank asked 2020-07-19T01:07:32Z
3个解决方案
99 votes

您可以将以下内容添加到Devise::Test::ControllerHelpers

RSpec.configure do |config|
  config.include Devise::Test::ControllerHelpers, type: :controller
end

这将包括所有:controller规格中的Devise::Test::ControllerHelpers模块。

lest answered 2020-07-19T01:07:50Z
11 votes

spec_helper.rb中添加:

config.include Devise::Test::ControllerHelpers, :type => :controller
Angel Huezo answered 2020-07-19T01:08:10Z
0 votes

MiniTest,Rails 4

这适用于香草Rails 4 MiniTest

test\controllers\users_controller_test.rb
require 'test_helper'

class UsersControllerTest < ActionController::TestCase
  include Warden::Test::Helpers
  include Devise::Test::ControllerHelpers

  setup do
    # https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
    # @user = users(:admin)
    # sign_in @user
  end

  teardown do
    Warden.test_reset!
  end

  test "login as admin" do
    @user = users :admin
    sign_in @user
    get :dashboard
    assert_redirected_to admin_dashboard_path
  end

end
Chloe answered 2020-07-19T01:08:39Z
translate from https://stackoverflow.com:/questions/38421853/why-is-my-rspec-not-loading-devisetestcontrollerhelpers