Login Devise user in Capybara tests
11 March 2022 (Updated 23 April 2022)
These instructions assume you’ve implemented authentication using Devise and you’re using RSpec & Capybara to write your feature tests.
Include Warden test helpers
Create a spec/support/devise.rb
file with the following configuration:
# frozen_string_literal: true
RSpec.configure do |config|
config.include Warden::Test::Helpers
end
Include this support file in spec/rails_helper.rb
:
require 'support/devise'
Use the login_as
helper method
Now, you 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)
login_as(user)
# Session will now be authenticated
visit root_path
See Devise’s wiki page for more info on testing with Capybara.
Tagged:
Rails testing
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment