Ruby

Patrick Montalto
Patrick Montalto
7,868 Points

Minitest with Capybara log in method

#test/test_helper.rb

# Logs in a test user.
  def log_in_as(user, options = {})
    password    = options[:password]    || 'password'
    remember_me = options[:remember_me] || '1'
    if integration_test?
      post login_path, session: { email:       user.email,
                                  password:    password,
                                  remember_me: remember_me }
    else
      session[:user_id] = user.id
    end
  end

#test/features/home_test.rb
require "test_helper"

def setup
  @user = users(:admin)
end

feature "Viewing the home page" do

  scenario "signing in successfully" do
    log_in_as(@user)
    visit root_path
  end
end