简体   繁体   English

尝试包含 Devise::Test::ControllerHelpers 时出错

[英]Error when trying to include Devise::Test::ControllerHelpers

I'm trying to get started with controller tests, and I'm not sure what I'm doing wrong.我正在尝试开始进行控制器测试,但我不确定我做错了什么。 Here's my code, and the error it's producing:这是我的代码,以及它产生的错误:

require 'test_helper'

class InvProcure::UserImportsControllerTest < ActionDispatch::IntegrationTest
  include Devise::Test::ControllerHelpers

  test "should get index" do
    user = users(:foobars_admin)
    sign_in(:user, user)
    get inv_procure_user_imports_path
    assert_response :success
  end
end
NoMethodError: undefined method `env' for nil:NilClass
    /home/blaine/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/devise-4.7.1/lib/devise/test/controller_helpers.rb:42:in `setup_controller_for_warden'

It looks like the error might be happening when including devise test helpers, or when calling sign_in .包含设计测试助手或调用sign_in时,似乎可能会发生错误。

From the doc: https://www.rubydoc.info/gems/devise/Devise/Test/ControllerHelpers来自文档: https ://www.rubydoc.info/gems/devise/Devise/Test/ControllerHelpers

Devise::Test::ControllerHelpers provides a facility to test controllers in isolation when using ActionController::TestCase allowing you to quickly sign_in or sign_out a user. Devise::Test::ControllerHelpers提供了在使用ActionController::TestCase时单独测试控制器的工具,允许您快速登录或注销用户。 Do not use Devise::Test::ControllerHelpers in integration tests.不要在集成测试中使用Devise::Test::ControllerHelpers

You are inheriting the integration test class instead of the controller test class which is the default for controller tests now (From Rails 5 the controller tests are generated with the parent class as ActionDispatch::IntegrationTest instead of ActionController::TestCase ).您正在继承集成测试类而不是控制器测试类,这是现在控制器测试的默认值(从 Rails 5 开始,控制器测试是使用父类作为ActionDispatch::IntegrationTest而不是ActionController::TestCase生成的)。 Devise::Test::ControllerHelpers was built for ActionController::TestCase and not for integration tests. Devise::Test::ControllerHelpers是为ActionController::TestCase而不是为集成测试而构建的。

You can instead try to use Devise::Test::IntegrationHelpers which should have similar methods for integration tests.您可以尝试使用Devise::Test::IntegrationHelpers ,它应该具有类似的集成测试方法。

Document: https://www.rubydoc.info/gems/devise/Devise/Test/IntegrationHelpers文档: https ://www.rubydoc.info/gems/devise/Devise/Test/IntegrationHelpers

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

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