Devise warden error thrown for authenticate_user in functional Rails test

寵の児 提交于 2019-11-29 02:23:40

Do as documentation says:

class ActionController::TestCase
  include Devise::TestHelpers
end

Particularly, don't put include Devise::TestHelpers into classActiveSupport::TestCase.

This happens when the Warden and/or Devise inclusions are missing or not added in the proper place. It's tempting to add them into test_helper.rb since that's where helpers typically go but that won't work properly for Devise.

See https://github.com/plataformatec/devise/issues/1029 for more details.

To solve this problem, include both the Devise helper and the Warden helper in controller's test class like this:

require 'test_helper'                                  
class UserControllerTest < ActionController::TestCase  
  include Devise::TestHelpers                          
  include Warden::Test::Helpers                        
  Warden.test_mode!                                    

  def teardown                                         
    Warden.test_reset!                                 
  end                                                  

  # test "the truth" do                               
  #   assert true
  # end
end

This is required for every controller that uses Devise's authentication.

EDIT: As mentioned in the comments below, moving include Warden:TestHelpers from spec_helper.rb (or test_helper.rb) to rails_helper.rb works too.

I got this error, but it was because I put my include Devise::TestHelpers outside of my class definition.

require 'test_helper'
include Devise::TestHelpers

class Admin::ObservationsControllerTest < ActionController::TestCase
  setup do
  ...

This threw the warden error for 3 of my 7 test cases. Moving the include inside the class definition fixed everything.

I know this is an old question now but I found the answer of the Devise wiki

https://github.com/plataformatec/devise/wiki/How-To%3a-Test-with-Capybara

This has a step by step guide that is too involde to re-create here, however the first step is to include the Warden test helpers

include Warden::Test::Helpers
Warden.test_mode!

Hope this helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!