I''m deep into functional testing after getting my unit testing ironed out (thanks to several helpful listers). Now I''ve run into an a roadblock that does not seem to be addressed in the Agile book. As suggested, I''ve implemented a login helper in test_helper.rb def login(email=''blah-7Ts6kVb0ZJk@public.gmane.org'', password=''blah'') post :attempt_login, :user => { :email => email, :password => password } assert_not_nil session[:user_id] assert_redirected_to :action => ''show'' end This was originally a user_controller_test.rb but since I''ll need to call it before any login protected testing can hope to succeed, I didn''t want to repeat myself. I thought I had figured this out myself until I reread the testing chapter in the Agile book and saw basically the same thing there, on page 159. :-) But...wait...there''s no controller definition there, just an action. It worked perfectly directly in the users_controller_test.rb file, and referencing the helper method worked from that same test. But it doesn''t work from the other controllers as they think it''s a method on their controller that is being posted to. I tried this, and it doesn''t work either. def login(email=''blah-7Ts6kVb0ZJk@public.gmane.org'', password=''blah'') post :controller => ''users'', :action => :attempt_login, :user => { :email => email, :password => password } assert_not_nil session[:user_id] assert_redirected_to :action => ''show'' end Any ideas? P.S. Googling this has been fruitless, and api.rubyonrails.com Where are the docs on the post method? -- -- Tom Mornini
Tom Mornini wrote:> I''m deep into functional testing after getting my unit testing > ironed out (thanks to several helpful listers). > > Now I''ve run into an a roadblock that does not seem to be > addressed in the Agile book. > > As suggested, I''ve implemented a login helper in test_helper.rb > > def login(email=''blah-7Ts6kVb0ZJk@public.gmane.org'', password=''blah'') > post :attempt_login, > :user => { :email => email, > :password => password } > > assert_not_nil session[:user_id] > assert_redirected_to :action => ''show'' > end > > This was originally a user_controller_test.rb but since I''ll > need to call it before any login protected testing can hope > to succeed, I didn''t want to repeat myself. I thought I had > figured this out myself until I reread the testing chapter in > the Agile book and saw basically the same thing there, on page > 159. :-) > > But...wait...there''s no controller definition there, just an action. > It worked perfectly directly in the users_controller_test.rb file, > and referencing the helper method worked from that same test. > > But it doesn''t work from the other controllers as they think it''s > a method on their controller that is being posted to. > > I tried this, and it doesn''t work either. > > def login(email=''blah-7Ts6kVb0ZJk@public.gmane.org'', password=''blah'') > post :controller => ''users'', > :action => :attempt_login, > :user => { :email => email, > :password => password } > > assert_not_nil session[:user_id] > assert_redirected_to :action => ''show'' > end > > Any ideas? > > P.S. Googling this has been fruitless, and api.rubyonrails.com > Where are the docs on the post method? >I don''t have a real solution, but in my app''s functional tests I''ve just stuck the equivalent of: @request.session[:user_id]= id_of_some_user_from_fixtures in the setup method. This allows you to go ahead and functional-test protected areas. I''d also like to know how to do what you initially asked though! -- Robert Jones
On Oct 17, 2005, at 10:54 AM, Robert Jones wrote:> Tom Mornini wrote: > >> But...wait...there''s no controller definition there, just an action. >> It worked perfectly directly in the users_controller_test.rb file, >> and referencing the helper method worked from that same test. >> >> But it doesn''t work from the other controllers as they think it''s >> a method on their controller that is being posted to. >> >> I tried this, and it doesn''t work either. >> >> def login(email=''blah-7Ts6kVb0ZJk@public.gmane.org'', password=''blah'') >> post :controller => ''users'', >> :action => :attempt_login, >> :user => { :email => email, >> :password => password } >> >> assert_not_nil session[:user_id] >> assert_redirected_to :action => ''show'' >> end >> >> Any ideas? >> >> P.S. Googling this has been fruitless, and api.rubyonrails.com >> Where are the docs on the post method? >> >> > > I don''t have a real solution, but in my app''s functional tests I''ve > just > stuck the equivalent of: > > @request.session[:user_id]= id_of_some_user_from_fixtures > > in the setup method. > > This allows you to go ahead and functional-test protected areas. > > I''d also like to know how to do what you initially asked though!Thanks, Robert. That was a highly pragmatic solution! Hopefully someone will give us a complete solution, but yours will let me keep moving for now. -- -- Tom Mornini
Hi Tom, Just a thought as I was reading the AWDR book again, have you tried doing something like this: def test_something_that_requires_2_controllers controller1 = Controller1.new controller2 = Controller2.new @controller = controller1 post :action => action1 #some assertions @controller = controller2 post :action => action2 #some assertions End -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Tom Mornini Sent: Tuesday, 18 October 2005 5:02 AM To: jonesieboy-FhtRXb7CoQBt1OO0OYaSVA@public.gmane.org; rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Re: How to post to specific controller/action On Oct 17, 2005, at 10:54 AM, Robert Jones wrote:> Tom Mornini wrote: > >> But...wait...there''s no controller definition there, just an action. >> It worked perfectly directly in the users_controller_test.rb file, >> and referencing the helper method worked from that same test. >> >> But it doesn''t work from the other controllers as they think it''s a >> method on their controller that is being posted to. >> >> I tried this, and it doesn''t work either. >> >> def login(email=''blah-7Ts6kVb0ZJk@public.gmane.org'', password=''blah'') >> post :controller => ''users'', >> :action => :attempt_login, >> :user => { :email => email, >> :password => password } >> >> assert_not_nil session[:user_id] >> assert_redirected_to :action => ''show'' >> end >> >> Any ideas? >> >> P.S. Googling this has been fruitless, and api.rubyonrails.com >> Where are the docs on the post method? >> >> > > I don''t have a real solution, but in my app''s functional tests I''ve > just stuck the equivalent of: > > @request.session[:user_id]= id_of_some_user_from_fixtures > > in the setup method. > > This allows you to go ahead and functional-test protected areas. > > I''d also like to know how to do what you initially asked though!Thanks, Robert. That was a highly pragmatic solution! Hopefully someone will give us a complete solution, but yours will let me keep moving for now. -- -- Tom Mornini _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Have not, but will soon! Thanks! P.S. It turns out, after digging through the code, that the Rails guys covered my case. post and get both funnel to process, which takes 3 args, action, params, session. So now I''m just passing the session hash the way I want it, -- -- Tom Mornini On Oct 20, 2005, at 11:24 PM, Neville Burnell wrote:> Hi Tom, > > Just a thought as I was reading the AWDR book again, have you tried > doing something like this: > > def test_something_that_requires_2_controllers > controller1 = Controller1.new > controller2 = Controller2.new > > @controller = controller1 > post :action => action1 > > #some assertions > > @controller = controller2 > post :action => action2 > > #some assertions > End > > > > > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Tom Mornini > Sent: Tuesday, 18 October 2005 5:02 AM > To: jonesieboy-FhtRXb7CoQBt1OO0OYaSVA@public.gmane.org; rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] Re: How to post to specific controller/action > > On Oct 17, 2005, at 10:54 AM, Robert Jones wrote: > > >> Tom Mornini wrote: >> >> >>> But...wait...there''s no controller definition there, just an action. >>> It worked perfectly directly in the users_controller_test.rb file, >>> and referencing the helper method worked from that same test. >>> >>> But it doesn''t work from the other controllers as they think it''s a >>> method on their controller that is being posted to. >>> >>> I tried this, and it doesn''t work either. >>> >>> def login(email=''blah-7Ts6kVb0ZJk@public.gmane.org'', password=''blah'') >>> post :controller => ''users'', >>> :action => :attempt_login, >>> :user => { :email => email, >>> :password => password } >>> >>> assert_not_nil session[:user_id] >>> assert_redirected_to :action => ''show'' >>> end >>> >>> Any ideas? >>> >>> P.S. Googling this has been fruitless, and api.rubyonrails.com >>> Where are the docs on the post method? >>> >>> >>> >> >> I don''t have a real solution, but in my app''s functional tests I''ve >> just stuck the equivalent of: >> >> @request.session[:user_id]= id_of_some_user_from_fixtures >> >> in the setup method. >> >> This allows you to go ahead and functional-test protected areas. >> >> I''d also like to know how to do what you initially asked though! >> > > Thanks, Robert. That was a highly pragmatic solution! > > Hopefully someone will give us a complete solution, but yours will let > me keep moving for now. > > -- > -- Tom Mornini > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >