0

I'm using rspec-rails to test my application and devise for my authentication solution.

As a lot of us, I ended up with the common error : undefined method authenticate' for nil:NilClass which I resolved partially by adding this line as suggested in the README :

config.include Devise::TestHelpers, type: :controller

The error goes away for controller's tests, but it was still there for my view's tests. I finally completely resolved it by adding :

config.include Devise::TestHelpers, type: :view

But I'm not sure if it's the right things to do since I've never seen it before anywhere.

Also, can we include it for every type like config.include Devise::TestHelpers, type: :all ?

Thanks for the clarification

Community
  • 1
  • 1
ZazOufUmI
  • 3,212
  • 6
  • 37
  • 67

1 Answers1

1

You can include a module in all examples by not specifying type.

config.include Devise::TestHelpers

If your views are using Devise's helpers, like current_user or user_signed_in?, including TestHelpers in their specs is reasonable. Though architecturally, you might want to consider assigning things like @user = current_user in your controller so you can decouple the views from authentication.

Kristján
  • 18,165
  • 5
  • 50
  • 62