Gerard
Gerard

Reputation: 4848

Devise warden error thrown for authenticate_user in functional Rails test

I have a resource where the new action requires a user to be logged in to view. If a user tries to create a new resource without being logged in, they are redirected (302'd) to the login page. My functional test looks like this:

  test "should not get new unless logged in" do
    get :new
    assert_response :redirect
  end

The stacktrace looks something like this:

ArgumentError: uncaught throw :warden
    /.../gems/warden-1.1.1/lib/warden/proxy.rb:114:in `throw'
    /.../gems/ruby-1.9.2-p318/gems/warden-1.1.1/lib/warden/proxy.rb:114:in `authenticate!'
    /.../gems/ruby-1.9.2-p318/gems/devise-2.0.4/lib/devise/controllers/helpers.rb:48:in `authenticate_user!'

I have a before_filter to authenticate_user before the new action.

I understand why authenticate_user! is failing but I can't understand why its throwing an error. Shouldn't it just behave as it does in the webapp ie. redirects user to the log in page?

Thanks.

Upvotes: 7

Views: 7143

Answers (4)

myagi
myagi

Reputation: 71

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.

Upvotes: 1

x-yuri
x-yuri

Reputation: 18973

Do as documentation says:

class ActionController::TestCase
  include Devise::TestHelpers
end

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

Upvotes: 5

xeorem
xeorem

Reputation: 593

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.

Upvotes: 2

TheLukeMcCarthy
TheLukeMcCarthy

Reputation: 2283

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

Upvotes: 0

Related Questions