Configuring Warden for use in RSpec controller specs
- by Chris Peters
I was able to use Devise's sign_in method to log in a user in my controller specs. But now that I'm removing Devise from my application, I'm not quite sure how to get similar functionality working with just Warden on its own.
How should I go about setting up spec/spec_helper.rb and related spec/support/*.rb files to get Warden running within controller specs sufficiently?
I've tried setting up a file at spec/support/warden.rb with these contents:
RSpec.configure do |config|
config.include Warden::Test::Helpers
config.after do
Warden.test_reset!
end
end
Then I have before calls similar to this to authenticate a user factory:
before { login_as FactoryGirl.create(:user) }
But here is the error that I keep seeing:
NameError:
undefined method `user' for nil:NilClass
This error traces back to my authenticate_user! method in the controller:
def authenticate_user!
redirect_to login_path, notice: "You need to sign in or sign up before continuing." if env['warden'].user.nil?
end
I'd appreciate any guidance that anyone could provide.