Getting ArgumentError: uncaught throw :warden with Devise

Recently, I was getting ArgumentError: uncaught throw :warden when I tried to test that user is being redirected if he wasn’t logged in and tried to access a secure section.

This was the test I wrote for it :-

test "should be redirected if index is called when logged out" do
  get :index
  assert_response :redirect
end

I searched the net for a while and everyone advised to include Devise::TestHelpers, which I was including in my test_helper.rb file.

class ActiveSupport::TestCase
  include Devise::TestHelpers
end

Since I couldn’t find any answer, I included the Devise::TestHelpers just above my tests. To my surprise, It suddenly worked!

Reason: It worked because I was initially including it in ActiveSupport::TestCase class where as in controller tests, I was inheriting from ActionController::TestCase class.

So I made another class in test_helper.rb file and wrote it as

class ActionController::TestCase
  include Devise::TestHelpers
end

and everything started working smoothly.

For reference, take a look at this issue and its comments.