Hi list. Just starting to wrap my heads around the whole unit testing deal. I''ve run into a bit of a (small) problem when testing a LoginController. my main login method that I''m looking to test looks like this: def checkauth if currentuser = User.checkauth(@params["login"]["username"], @params["login"]["password"]) @session["user_id"] = currentuser.id @session["username"] = currentuser.username # some more stuff, like redirects on sucess etc end and my simple testcase looks like this: def test_session_vars process ''checkauth'', "username" => ''foo'', "password" => ''bar'' assert_session_has ''user_id'' assert_session_has ''username'' assert_session_has_no ''admin'' end All results in this semicryptic error: 1) Error: test_session_vars(LoginControllerTest): NoMethodError: undefined method `[]'' for nil:NilClass ./test/functional/login_controller_test.rb:5:in `rescue_action'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-0.9.0/lib/action_controller/rescue.rb:70:in `perform_action'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-0.9.0/lib/action_controller/base.rb:254:in `process'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-0.9.0/lib/action_controller/test_process.rb:192:in `process'' ./test/functional/login_controller_test.rb:16:in `test_session_vars'' By accident I''ve come to the conclusion that the reason for the error most likely is because of this in my controller method: User.checkauth(@params["login"]["username"], @params["login"]["password"]. If I change the @params values to omit the ["login"] part it works. So. My question basicly is, how can I add the "login" part to process()? I''ve tried what I thought would be the most obvious: process "checkauth", ["login"]["username"]=>''foo'', ..and so on, but that gives me a TypeError ("cannot convert String into Integer") Thank you in advance, johan PS. Thanks Steve Kellock for writing that testing tutorial PDF, it explains the concepts really well.. -- Johan Sørensen Professional Futurist www.johansorensen.com
On Thu, Nov 11, 2004 at 12:12:37PM +0100, Johan Sörensen wrote:> Hi list. > > Just starting to wrap my heads around the whole unit testing deal. > I''ve run into a bit of a (small) problem when testing a > LoginController. > > my main login method that I''m looking to test looks like this: > def checkauth > if currentuser = User.checkauth(@params["login"]["username"], > @params["login"]["password"]) > @session["user_id"] = currentuser.id > @session["username"] = currentuser.username > # some more stuff, like redirects on sucess etc > end > > and my simple testcase looks like this: > def test_session_vars > process ''checkauth'', "username" => ''foo'', "password" => ''bar''Hi, Make that: process :checkauth, ''login'' => {''username'' => ''foo'', ''password'' => ''bar''}> PS. Thanks Steve Kellock for writing that testing tutorial PDF, it > explains the concepts really well..Yeah, it rocks. I just went through a ton of my FT''s yesterday making them use these wonderful new features. Glorious. -Scott
On Thu, 11 Nov 2004 07:19:11 -0500, Scott Barron <scott-HDQKq3lYuGDk1uMJSBkQmQ@public.gmane.org> wrote:> Make that: > > process :checkauth, ''login'' => {''username'' => ''foo'', ''password'' => ''bar''}Excellent! Works like a charm. Thanks. On a (not quite) similar note, what was that thing I needed to change due to some bug in ./script/new_model that failed to plurize something? --johan
On Thu, Nov 11, 2004 at 01:28:21PM +0100, Johan Sörensen wrote:> On Thu, 11 Nov 2004 07:19:11 -0500, Scott Barron <scott-HDQKq3lYuGDk1uMJSBkQmQ@public.gmane.org> wrote: > > Make that: > > > > process :checkauth, ''login'' => {''username'' => ''foo'', ''password'' => ''bar''} > > Excellent! Works like a charm. Thanks. > > > On a (not quite) similar note, what was that thing I needed to change > due to some bug in ./script/new_model that failed to plurize > something?To fix the script, I think it''s around line 43 (for tarball 0.8.0 rails), change: table_name = ARGV.shift || file_name to table_name = ARGV.shift || Inflector.pluralize(file_name) To fix an existing model, move its fixtures directory and the create_fixtures line of its unit tests. -Scott
I''ll second (or third) those thoughts on Steve''s PDF, hoping to dive into unit testing myself some point soon. Is there somewhere I can read up on this bug - ie. what is it, what problems does it cause? On Thu, 11 Nov 2004 07:33:08 -0500, Scott Barron <scott-HDQKq3lYuGDk1uMJSBkQmQ@public.gmane.org> wrote:> On Thu, Nov 11, 2004 at 01:28:21PM +0100, Johan Sörensen wrote: > > On Thu, 11 Nov 2004 07:19:11 -0500, Scott Barron <scott-HDQKq3lYuGDk1uMJSBkQmQ@public.gmane.org> wrote: > > > Make that: > > > > > > process :checkauth, ''login'' => {''username'' => ''foo'', ''password'' => ''bar''} > > > > Excellent! Works like a charm. Thanks. > > > > > > On a (not quite) similar note, what was that thing I needed to change > > due to some bug in ./script/new_model that failed to plurize > > something? > > To fix the script, I think it''s around line 43 (for tarball 0.8.0 > rails), change: > > table_name = ARGV.shift || file_name > > to > > table_name = ARGV.shift || Inflector.pluralize(file_name) > > To fix an existing model, move its fixtures directory and the > create_fixtures line of its unit tests. > > -Scott > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Thu, 11 Nov 2004 12:59:43 +0000, On The Rails <ontherails-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there somewhere I can read up on this bug - ie. what is it, what > problems does it cause?Basicly, it seems like the new_model (in the 0.8 distrib) scripts doesn''t pluralize the subdirs in the ./test/fixtures/ directory, aswell as the create_fixtures() methods in the unit tests. As Scott says, the solution is simple; rename the the ./test/fixtures/* dirs to be plural and in the ./test/unit/*_test.rb files change (with "user" being a model name): @user = create_fixtures "user" to: @user = create_fixtures "users" (note ''s'' on the end) there is no step three! (afaik atleast) ;)
> Basicly, it seems like the new_model (in the 0.8 distrib) scripts > doesn''t pluralize the subdirs in the ./test/fixtures/ directory, > aswell as the create_fixtures() methods in the unit tests.This is fixed in CVS and will be part of the next version. -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://macromates.com/ -- TextMate: Code and markup editor (OS X) http://www.loudthinking.com/ -- Broadcasting Brain