我正在使用Rails 5,并设计了3.5.1.

阅读一本关于创建/测试API的好书,该API使用Desive身份验证.它是在Rails 5之前编写的,所以我 Select 不使用新的仅api版本.

这是我的测试...

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

and tried this -> include Devise::Test::ControllerHelpers

这没用,因为文件controller_helpers.rb不在我的项目中

我错过了什么?

谢谢

推荐答案

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

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

这将包括:controller个规格中的Devise::Test::ControllerHelpers个模块.

Ruby-on-rails相关问答推荐

Rails HotWire和View Components:涡轮框架不会取代内容

`heroku Open`和`heroku run rake db:Migrate`不工作(";没有这样的文件或目录";)

DateTime的最小数据提交格式

Rails 7,返回具有两个单独条件的活动记录关联,一个在父级,一个在子级

使用拆分成多个部分的种子文件进行Rails数据种植

Rails/Rspec中的测试文章创建

为什么是 json.partial!使用新的哈希语法传递本地时,jbuiler 模板中的行不执行?

验证以确保唯一性但忽略空值?

如何在生产中部署 resque worker?

Rails Mailer Net::OpenTimeout: execution expired 仅在生产服务器上出现异常

在 Rails 中,如何处理多个选中的复选框,只需拆分 , 或?

如何在 Rails 迁移中添加判断约束?

设计和强大的参数

Rails 5:无法从参数中检索哈希值

Rails 3.1 插件 gem、虚拟测试应用程序、rspec

Groovy/Grails :: Ruby/Rails :: 2011 框架状态

有没有办法列出所有 belongs_to 关联?

扩展 Devise SessionsController 以使用 JSON 进行身份验证

Ruby 1.9.2 和 Rails 3 无法打开 rails 控制台

将现有的 html.erb 转换为 Haml