简体   繁体   中英

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 .

From the doc: 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. Do not use Devise::Test::ControllerHelpers in integration tests.

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 ). Devise::Test::ControllerHelpers was built for ActionController::TestCase and not for integration tests.

You can instead try to use Devise::Test::IntegrationHelpers which should have similar methods for integration tests.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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