Rails Integration Test with shoulda, factory_girl, webrat, and authlogic

Rails Integration Test with shoulda, factory_girl, webrat, and authlogic

I had some issues trying to use a symbol

session[:user_credentials_id]

vs. a string

session["user_credentials_id"]

Here is my solution:

require 'test_helper'

class LoginTest < ActionController::IntegrationTest
  context 'A user' do
    setup do
      @user = Factory(:user)
      # puts @user.inspect    
    end

    should 'be able to login with a valid username and password' do
      visit '/user_session/new'
      assert_equal '/user_session/new', path
      fill_in 'Login', :with => @user.login
      fill_in 'Password', :with => @user.password
      click_button 'Login'
      # puts "user.id: #{@user.id}" 
      # puts "session: #{session.inspect}"
      # puts "session[:user_credentials_id] #{session["user_credentials_id"]}"
      assert_equal @user.id, session["user_credentials_id"]
    end

    should 'be denied access with an invalid username or password' do
      visit '/user_session/new'
      assert_equal '/user_session/new', path
      fill_in 'Login', :with => @user.login
      fill_in 'Password', :with => 'wrongpassord'
      click_button 'Login'
      assert_nil session["user_credentials_id"]
    end

  end
end

Comments

AWS migration and transformation programs reduce cost, risk, and complexity for rapid business innovation and agility at scale. I offer a number of AWS consulting services, including an AWS migration service to quickly get you up and running. Please contact me for details on how I can help your organization.