testing with authlogic

205 views
Skip to first unread message

Mike C

unread,
Mar 17, 2009, 4:52:01 AM3/17/09
to Authlogic
I'm trying to make tests now (integration) so I need to emulate an
actual login. I tried looking at various examples like in the test app
and such, but nothing has worked so far. Has anyone successfully ran
integration tests with authlogic? I've been trying to figure this out
for a while. Thanks!

Ariejan de Vroom

unread,
Mar 17, 2009, 6:38:03 AM3/17/09
to auth...@googlegroups.com
Hey Mike, 

I'm using Cucumber for this kind of stuff, and it works perfectly by just filling in the login form ;-) 
There's also a test_helper available which will give you set_session_for(). All you need to do is require the file in your spec_helper (or test_helper for that matter). 

 - Ariejan

Mike C

unread,
Mar 17, 2009, 1:27:36 PM3/17/09
to Authlogic
Thanks, I checked it out and it looks interesting. Do you think you
could give me a simple example using this with authlogic, or do you
know of a place I could find some examples?

On Mar 17, 3:38 am, Ariejan de Vroom <arie...@gmail.com> wrote:
> Hey Mike,
> I'm using Cucumber for this kind of stuff, and it works perfectly by just
> filling in the login form ;-) There's also a test_helper available which
> will give you set_session_for(). All you need to do is require the file in
> your spec_helper (or test_helper for that matter).
>
>  - Ariejan
>

Hector Gomez

unread,
Mar 19, 2009, 10:00:09 PM3/19/09
to Authlogic
HI:

I am doing a base template that configures cucumber, rspec, forgery,
etc and implements a basic authentication system using authlogic.
Right now I have finished doing the registration features and I
working right now in the authentication features.
I have some basic code for a login in spec test for users controllers.

http://github.com/hectoregm/rails-templates/tree/master

Railsmaniac

unread,
Mar 20, 2009, 10:10:56 AM3/20/09
to Authlogic
I have some code that emulates authentication in my controller tests.
Don't know, if it's good to use, but:

# returns current fake user
def current_user
@current_user ||= Factory(:user)
end

# returns current session mock
def user_session
@user_session = mock
@user_session.stubs(:user).returns(current_user)
@user_session.stubs(:record)
@user_session
end

# logges me in
def login
UserSession.stubs(:find).returns( user_session )
end

# logges me out
def logout
@user_session = nil
end

That works fine for me right now, but still, should be improved.

schwabsauce

unread,
Mar 20, 2009, 1:14:00 PM3/20/09
to Authlogic
do you remain logged in after you go to another controller?

I wasn't able to get that set_session_for helper to work with rspec
but I might keep trying

Hector Gomez

unread,
Mar 20, 2009, 10:30:53 PM3/20/09
to Authlogic
I have a rspec compatible code for the authlogic test helpers they are
in
base/spec/lib directory its named spec_authlogic_helper.rb in my
github repository
check it out

Railsmaniac

unread,
Mar 21, 2009, 9:47:41 AM3/21/09
to Authlogic
Yauch. Doesn't work with cucumber, or others. Doesn't remain logged. :
(

Ben Johnson

unread,
Mar 21, 2009, 1:25:42 PM3/21/09
to auth...@googlegroups.com
I don't use rspec, so if someone can come up with a nice tool set that
works, I will throw it in the library and we can save a lot of people
time. I use plain old test unit and the helpers I have work great.



Ben Johnson
Binary Logic

W: www.binarylogic.com
E: bjoh...@binarylogic.com

1430 Broadway
7th Floor - NECO
New York, NY 10018

Ariejan de Vroom

unread,
Mar 21, 2009, 3:30:38 PM3/21/09
to auth...@googlegroups.com
Hey Ben, 

Using the supplied test_helper with Rspec works fine. Just create a User and use 

  set_session_for(@user)

This works fine, since I haven't got any trouble with it at all. 

For cucumber I recommend you stick with using your front-end, so create a user and *go to the login form and login*. I'm using cucumber with webrat this way and it works flawlessly. 

Regards, 

Ariejan
Reply all
Reply to author
Forward
0 new messages