为什么我的RSpec没有加载Devise :: Test :: ControllerHelpers?

时间:2016-07-17 13:40:44

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

我正在使用Rails 5和Devise 3.5.1。

阅读一本关于创建/测试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在我的项目中无处可用

我在这里想念的是什么?

由于

3 个答案:

答案 0 :(得分:92)

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

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

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

答案 1 :(得分:11)

spec_helper.rb添加:

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

答案 2 :(得分:0)

MiniTest,Rails 4

这适用于Vanilla Rails 4 MiniTest

Sink is unwriteable by current module (line 7) Both left and right are drivers (line 8)
test\controllers\users_controller_test.rb