sajad torkamani

Assuming we are using RSpec & Capybara to write our feature tests and utilizing Devise for authentication, we can login users in our tests like so:

Include Warden test helpers

Create a spec/support/devise.rb file with the following configuration:

RSpec.configure do |config|
  config.include Warden::Test::Helpers

Include this support file in spec/rails_helper.rb:

require 'support/devise'

Use the login_as helper method

Now we can use the login_as method to log in any ActiveRecord user like so:

# Create user however you want (e.g. using Factory Bot)
user = FactoryBot.create(:user)

# Session will now be authenticated
visit root_path

See Devise's wiki page for more info on testing with Capybara.

Tagged: Rails