Forum: Ruby on Rails Login test method (from Agile book)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
hughes.james (Guest)
on 2005-11-12 13:49
(Received via mailing list)

I'm trying to use the method detailed on page 159 of the pragmatic
programmers book to allow functional tests to login to an app
protected with the salted login generator and before_filter. So,  I
have a login method in test_helper.rb that does a post to the login
action using a user that is created with a fixture. In application.rb,
I have

  before_filter :login_required, :except => [:login, :logout, :signup,

In a test method, I have "@user = User.find_by_login("bob")" in the
setup method, and "login(@user.login)" in all the test methods. (Not
very DRY, but this is what AWDR recommends.)

The login method is supposed to redirect to 'index' on successful
login, but the test always fails like this:

  1) Failure:
    [./test/functional/../test_helper.rb:18:in `login'
     ./test/functional/datasets_controller_test.rb:56:in `test_create']:
response is not a redirection to all of the options supplied
(redirection is <{:controller=>"/user",  :action=>"login"}>)

The kicker is that, if I put a breakpoint in the real login method,
the call to User.authenticate always succeeds; hence this code:
      redirect_back_or_default :action => 'index'
always executes.

btw, I've tried the alternate method of just plopping a bogus user in
the session that has been suggested in other threads. before_filter
prevents this from working as well.


James H.
Web application developer
Centre for Health Services and Policy Research
Vancouver, BC
This topic is locked and can not be replied to.