Login test method (from Agile book)

Hi,

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,
:change_password]

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_create(DatasetsControllerTest)
    [./test/functional/…/test_helper.rb:18:in login' ./test/functional/datasets_controller_test.rb:56:intest_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.

jh


James H.
Web application developer
Centre for Health Services and Policy Research
Vancouver, BC