【问题标题】:Why is my RSpec not loading Devise::Test::ControllerHelpers?为什么我的 RSpec 没有加载 Devise::Test::ControllerHelpers?
【发布时间】:2016-11-20 04:24:18
【问题描述】:

我正在使用 Rails 5 和 Devise 3.5.1。

阅读一本关于创建/测试 API 的不错的(较旧的)书籍,该 API 使用 Devise 身份验证。它是在 Rails 5 之前编写的,所以我选择不使用新的 api-only 版本。

这是我的测试...

#/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 不在我的项目中

我错过了什么?

谢谢

【问题讨论】:

  • 根据这个答案,您不能在控制器测试中使用 Warden/Devise (ActionController::TestCase),因为它是 Rack 中间件,并且没有为控制器测试加载 Rack 中间件。 stackoverflow.com/questions/13420923/…

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


【解决方案1】:

您可以将以下内容添加到您的rails_helper

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

这将在所有:controller 规范中包含Devise::Test::ControllerHelpers 模块。

【讨论】:

  • 好吧,这是有道理的。谢谢先生。 type: :controller 只是为了在 RSpec 运行其他类型的测试时节省内存吗?
  • Devise::Test::ControllerHelpers 仅用于控制器测试,因此我认为如果将其包含在所有其他规范中,则可能会破坏其他一些规范。这就是为什么我们需要指定type: :controller
【解决方案2】:

spec_helper.rb添加:

config.include Devise::Test::ControllerHelpers, :type => :controller

【讨论】:

    【解决方案3】:

    MiniTest,Rails 4

    这适用于 vanilla 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-24
      • 2012-01-09
      • 1970-01-01
      • 2016-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      相关资源
      最近更新 更多