【发布时间】: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