views:

195

answers:

1

Is there any way to logout and login another user when testing with Authlogic?

The following test case just fails

class MessagesControllerTest < ActionController::TestCase
  setup :activate_authlogic

  should "sender send a message and recipient delete it" do
    @sender = Factory(:user, :login => 'sender')
    @recipient = Factory(:user, :login => 'recipient')

    UserSession.create(@sender)
    post :create, :user_id => @recipient.id, :message => Factory.attributes_for(:message)
    assert_equal @sender, assigns(:current_user)

    UserSession.create(@recipient)
    post :destroy, :user_id => @recipient.id, :message => @message
    assert_equal @recipient, assigns(:current_user)
  end
end

The crazy thing is that request session params look ok...

# UserSession.create(@sender)
"user_credentials"=>   "605a73e0f1b4dcecf5495d549b9d33eb6d3197f3a1fdb033c86bca456a0a0c75a42b63c924edb5d071f6d748aea3870f312a908eb501e6814c69abdab9bbf387",
"user_credentials_id"=>1


# UserSession.create(@recipient)
"user_credentials"=> "e14363b2065b7202e766205e807d6b531e230a136c70ea13c0853c7c0f275fb95f0185b9f17a934483cadda693e50956d1bfda8c820895f574a06861479af18c",
"flash"=>{:notice=>"Message was successfully created."},
"user_credentials_id"=>2
A: 

What about session = UserSession.create(@sender) and session.destroy?

Leventix
still fails... :S there are many issues here, first Factory(:user) is automatically logging-in the user, so `UserSession.create(@sender)` is useless. Second one is the way authlogic is mocking up the request, the first one works but not the second one.
knoopx
Taking a second look at it, this should be an integration test and not functional. You would not have this problem in an integration test if you simulate all the logging in/out process as requests.A functional test case should only contain one request imo.
Leventix