简体   繁体   English

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

[英]Why is my RSpec not loading Devise::Test::ControllerHelpers?

I'm using Rails 5, and Devise 3.5.1. 我正在使用Rails 5和Devise 3.5.1。

Going through a nice (older) book about creating/testing an API, which uses Devise authentication. 阅读一本关于创建/测试API的好(旧)书,该书使用Devise身份验证。 It was written before Rails 5, so I chose not to use the new api-only version. 它是在Rails 5之前编写的,所以我选择不使用新的api-only版本。

Here's my test... 这是我的测试......

#/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

And here's a completely unexpected RSpec error 这是一个完全出乎意料的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.

So I go here - http://www.rubydoc.info/gems/devise/Devise/Test/ControllerHelpers 所以我去了这里 - http://www.rubydoc.info/gems/devise/Devise/Test/ControllerHelpers

and tried this -> include Devise::Test::ControllerHelpers 并尝试了这个 - > 包括Devise :: Test :: ControllerHelpers

which didn't help because the file controller_helpers.rb is nowhere in my project 这没有帮助,因为文件controller_helpers.rb在我的项目中无处可去

What did I miss here? 我在这里想念的是什么?

Thanks 谢谢

You could add the following to your rails_helper : 您可以将以下内容添加到rails_helper

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

This will include Devise::Test::ControllerHelpers module in all :controller specs. 这将包括所有:controller规范中的Devise::Test::ControllerHelpers模块。

spec_helper.rb添加:

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

MiniTest, Rails 4 MiniTest,Rails 4

This works for vanilla Rails 4 MiniTest 这适用于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM