Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional testing Authlogic?

In a few of my controllers I have a before_filter that checks if a user is logged in? for CRUD actions.

application.rb

def logged_in?
  unless current_user
    redirect_to root_path
  end
end

private
def current_user_session
  return @current_user_session if defined?(@current_user_session)
  @current_user_session = UserSession.find
end

def current_user
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.record
end

But now my functional tests fail because its redirecting to root. So I need a way to simulate that a session has been created but nothing I've tried has worked. Heres what I have right now and the tests pretty much ignore it:

test_helper.rb

class ActionController::TestCase
  setup :activate_authlogic
end

posts_controller_test.rb

class PostsControllerTest < ActionController::TestCase
  setup do
    UserSession.create(:username => "dmix", :password => "12345")
  end

  test "should get new" do  
    get :new
    assert_response :success
  end

Am I missing something?

like image 255
kush Avatar asked Jun 16 '09 01:06

kush


1 Answers

You should pass ActiveRecord object in UserSession.create

Something like:

u = users(:dmix)
UserSession.create(u)
like image 68
Anton Mironov Avatar answered Sep 29 '22 14:09

Anton Mironov