Tuesday, February 28, 2012

Authlogic login access in rails functional test

We are shifted to use Authlogic, Devise rather than rails authorization plugin/gem. We can do Integration with detailed explanations/tutorials available for Authlogic.

In Rails test (functional/integration test in particular), we are in need to test whether the user is logged-in or not.
To start testing the user session, we need to use authlogic session. By using the following steps we can access Authlogic session in our rails test

- Add the following line in your test_helper.rb
require "authlogic/test_case"

- And add the method in your test_helper.rb
def user_login(user)
UserSession.create(user ? user : nil)
end

- Then inside your functional/integration test file
Add this to top of all test
setup :activate_authlogic

- Logged in with any existing user
user_login(users(:last))

You are logged in.
- User session is accessible.

Web application testing is much easier in Rails :)

Cheers,
Vadivelan.K

No comments:

Post a Comment