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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E74a19a983b0da1c671de90b341e7c06?d=identicon&s=25 hughes.james (Guest)
on 2005-11-12 12:49
(Received via mailing list)
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: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.

jh


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