All Ruby tests raising: undefined method `authenticate' for nil:NilClass

39,553

Solution 1

This question has been answered on Twitter by @MatthewClosson

@jeffehh You need to create a spec/support/devise.rb file as specified here https://github.com/plataformatec/devise#test-helpers to include the devise test helpers #ruby

Thanks once again.

Solution 2

I'm aware you're using Rspec but you can run into this same issue with Test::Unit. You just need to add the devise test helpers to test/test_helper.rb

class ActiveSupport::TestCase
  include Devise::TestHelpers
end

Solution 3

in RSpec

as Jeffrey W. mentioned, in his answer above -> to set this to all controllers:

RSpec.configure do |config|
  # ...
  config.include Devise::TestHelpers, type: :controller
  # ...
end

however, if this is relevant to just one spec, you don't necessarily need to include devise helpers to all your controllers specs, you can just explicitly include those helpers in that one controller describe block:

require 'spec_helper'
describe MyCoolController
  include Devise::TestHelpers

  it { } 
end

Solution 4

The above answer did not work for me (RSpec 3.1)

See https://stackoverflow.com/a/21166482/1161743 for a solution that worked for me.

You will need to log out an anonymous user before setting up variables:

before :each do
  sign_out :user
end

Solution 5

I was experiencing the same failures in one of my projects. It is using Ruby 2.0.0-p598, Rails 3.2.21, RSpec 2.99. When I run all specs together the problem occurred. When I ran the specs individually, they passed. I have the following included in my spec/spec_helper.rb:

RSpec.configure do |config|
  # ...
  config.include Devise::TestHelpers, type: :controller
  # ...
end

I added the following to the first describe in each failing spec file. This did not resolve the problem:

before :each do
  sign_out :user
end

Neither did:

after :each do
  sign_out :user
end

Taking inspiration from the answer to this stackoverflow question, I ran different combinations of rspec directories together to find out which ones could be interfering with each other. In the end I discovered I was calling:

before() do #note no :each passed to before
  :
end

when I changed all occurrences of this to:

before(:each) do
  :
end

All the specs passed without the failure:

undefined method `authenticate' for nil:NilClass

I hope this is of help to others.

Share:
39,553
Jeffrey W.
Author by

Jeffrey W.

Updated on July 14, 2022

Comments

  • Jeffrey W.
    Jeffrey W. almost 2 years

    Most of my tests are raising the following and I don't understand why. All methods call raise the 'authenticate' error. I've checked the code if there was a method called "authenticate" but there is no such method.

      1) Admin::CommentsController handling GET to index is successful
         Failure/Error: get :index
         undefined method `authenticate!' for nil:NilClass
         # ./spec/controllers/admin/comments_controller_spec.rb:9:in `block (3 levels) in <top (required)>'
    
    
      124) PostsController handling GET for a single post should render show template
         Failure/Error: get :show, :year => '2008', :month => '01', :day => '01', :slug => 'a-post'
         undefined method `authenticate' for nil:NilClass
         # ./app/controllers/application_controller.rb:18:in `set_current_user_for_model'
         # ./spec/controllers/posts_controller_spec.rb:131:in `do_get'
         # ./spec/controllers/posts_controller_spec.rb:140:in `block (3 levels) in <top (required)>'
    

    The project can be found over there => https://github.com/agilepandas/enki in case you'd like to run the tests your self.

  • Zabba
    Zabba over 13 years
    Logging actual URL (instead of a bit.ly) : on the devise page (github.com/plataformatec/devise), see the section "Test Helpers".
  • Tiago Peczenyj
    Tiago Peczenyj about 10 years
    please don't do this. nil is nil, is not a valid object in 90% of the cases.
  • max
    max almost 9 years
    This answer is about 99% pure nonsense.
  • Joseph Siefers
    Joseph Siefers over 8 years
    Also, check that you haven't included Devise::TestHelpers multiple times as this can cause issues.