为什么我的 RSpec 没有加载 Devise::Test::ControllerHelpers? [英] Why is my RSpec not loading Devise::Test::ControllerHelpers?
问题描述
我使用的是 Rails 5 和 Devise 3.5.1.
I'm using Rails 5, and Devise 3.5.1.
阅读一本关于创建/测试使用 Devise 身份验证的 API 的不错(较旧)的书.它是在 Rails 5 之前编写的,所以我选择不使用新的 api-only 版本.
Going through a nice (older) book about creating/testing an API, which uses Devise authentication. It was written before Rails 5, so I chose not to use the new api-only version.
这是我的测试...
#/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 错误
And here's a completely unexpected RSpec error
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/测试/控制器助手
并尝试了这个 -> include Devise::Test::ControllerHelpers
这没有帮助,因为文件 controller_helpers.rb
不在我的项目中
which didn't help because the file controller_helpers.rb
is nowhere in my project
我在这里错过了什么?
谢谢
推荐答案
您可以将以下内容添加到您的 rails_helper
:
You could add the following to your rails_helper
:
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
end
这将包括所有 :controller
规范中的 Devise::Test::ControllerHelpers
模块.
This will include Devise::Test::ControllerHelpers
module in all :controller
specs.
这篇关于为什么我的 RSpec 没有加载 Devise::Test::ControllerHelpers?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!