Karl
2009-Mar-13 03:38 UTC
Understanding how methods are made available within controller tests
Being new to rails and ruby, I am trying to figure out how things fit together. In particular i am trying to understand how attributes such as "session" and "cookies" are made available to Controller tests. Specifically I am working on a test from the RailsSpace book which extends Test::Unit::TestCase and only imports the module ApplicationHelper while attributes such as cookies and session are defined within the ActionController::TestProcess module. I cannot figure out how module ActionController::TestProcess is included within the UserControllerTest. require File.dirname(__FILE__) + ''/../test_helper'' require ''user_controller'' # Re-raise errors caught by the controller. class UserController; def rescue_action(e) raise e end; end class UserControllerTest < Test::Unit::TestCase include ApplicationHelper .. .. def authorize(user) @request.session[:user_id] = user.id end def friendly_url_forwarding_aux(test_page, protected_page, user) get protected_page assert_response :redirect assert_redirected_to :action => "login" post test_page, :user => user assert_response :redirect assert_redirected_to :action => protected_page assert_nil session[:protected_page] end def cookie_value(symbol) cookies[symbol.to_s].value.first end def cookie_expires(symbol) cookies[symbol.to_s].expires end thx. -karl --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Mar-13 09:53 UTC
Re: Understanding how methods are made available within controller tests
On Mar 13, 3:38 am, Karl <karl.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Being new to rails and ruby, I am trying to figure out how things fit > together. In particular i am trying to understand how attributes such > as "session" and "cookies" are made available to Controller tests. > Specifically I am working on a test from the RailsSpace book which > extends Test::Unit::TestCase and only imports the module > ApplicationHelper while attributes such as cookies and session are > defined within the ActionController::TestProcess module. I cannot > figure out how module ActionController::TestProcess is included within > the UserControllerTest.The test_helper file that you require at the top of your test file in turn requires a bunch of stuff, in particular a file that adds those methods to test cases. Fred> > require File.dirname(__FILE__) + ''/../test_helper'' > require ''user_controller'' > > # Re-raise errors caught by the controller. > class UserController; def rescue_action(e) raise e end; end > > class UserControllerTest < Test::Unit::TestCase > include ApplicationHelper > > .. > .. > def authorize(user) > @request.session[:user_id] = user.id > end > > def friendly_url_forwarding_aux(test_page, protected_page, user) > get protected_page > assert_response :redirect > assert_redirected_to :action => "login" > post test_page, :user => user > assert_response :redirect > assert_redirected_to :action => protected_page > assert_nil session[:protected_page] > end > > def cookie_value(symbol) > cookies[symbol.to_s].value.first > end > > def cookie_expires(symbol) > cookies[symbol.to_s].expires > end > > thx. > > -karl--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Karl Baum
2009-Mar-13 17:55 UTC
Re: Understanding how methods are made available within controller tests
Makes sense. My only confusion is I thought that in ruby methods and attributes can be imported to a class either through inheritance or importing a module. In this case, these extra attributes such as "session" or "cookies" do not exist within the parent class nor the imported module. Is rails including extra modules within my classes at runtime? Thanks for your help! -karl On Fri, Mar 13, 2009 at 5:53 AM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Mar 13, 3:38 am, Karl <karl.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Being new to rails and ruby, I am trying to figure out how things fit > > together. In particular i am trying to understand how attributes such > > as "session" and "cookies" are made available to Controller tests. > > Specifically I am working on a test from the RailsSpace book which > > extends Test::Unit::TestCase and only imports the module > > ApplicationHelper while attributes such as cookies and session are > > defined within the ActionController::TestProcess module. I cannot > > figure out how module ActionController::TestProcess is included within > > the UserControllerTest. > > The test_helper file that you require at the top of your test file in > turn requires a bunch of stuff, in particular a file that adds those > methods to test cases. > > Fred > > > > require File.dirname(__FILE__) + ''/../test_helper'' > > require ''user_controller'' > > > > # Re-raise errors caught by the controller. > > class UserController; def rescue_action(e) raise e end; end > > > > class UserControllerTest < Test::Unit::TestCase > > include ApplicationHelper > > > > .. > > .. > > def authorize(user) > > @request.session[:user_id] = user.id > > end > > > > def friendly_url_forwarding_aux(test_page, protected_page, user) > > get protected_page > > assert_response :redirect > > assert_redirected_to :action => "login" > > post test_page, :user => user > > assert_response :redirect > > assert_redirected_to :action => protected_page > > assert_nil session[:protected_page] > > end > > > > def cookie_value(symbol) > > cookies[symbol.to_s].value.first > > end > > > > def cookie_expires(symbol) > > cookies[symbol.to_s].expires > > end > > > > thx. > > > > -karl > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Mar-14 12:47 UTC
Re: Understanding how methods are made available within controller tests
On Mar 13, 5:55 pm, Karl Baum <karl.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Makes sense. My only confusion is I thought that in ruby methods and > attributes can be imported to a class either through inheritance or > importing a module. In this case, these extra attributes such as "session" > or "cookies" do not exist within the parent class nor the imported module. > Is rails including extra modules within my classes at runtime? > Thanks for your help! >You can also just reopen a class and add methods to it. if my memory is correct that is what rails does to Test::Unit::TestCase. Fred> -karl > > On Fri, Mar 13, 2009 at 5:53 AM, Frederick Cheung < > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Mar 13, 3:38 am, Karl <karl.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Being new to rails and ruby, I am trying to figure out how things fit > > > together. In particular i am trying to understand how attributes such > > > as "session" and "cookies" are made available to Controller tests. > > > Specifically I am working on a test from the RailsSpace book which > > > extends Test::Unit::TestCase and only imports the module > > > ApplicationHelper while attributes such as cookies and session are > > > defined within the ActionController::TestProcess module. I cannot > > > figure out how module ActionController::TestProcess is included within > > > the UserControllerTest. > > > The test_helper file that you require at the top of your test file in > > turn requires a bunch of stuff, in particular a file that adds those > > methods to test cases. > > > Fred > > > > require File.dirname(__FILE__) + ''/../test_helper'' > > > require ''user_controller'' > > > > # Re-raise errors caught by the controller. > > > class UserController; def rescue_action(e) raise e end; end > > > > class UserControllerTest < Test::Unit::TestCase > > > include ApplicationHelper > > > > .. > > > .. > > > def authorize(user) > > > @request.session[:user_id] = user.id > > > end > > > > def friendly_url_forwarding_aux(test_page, protected_page, user) > > > get protected_page > > > assert_response :redirect > > > assert_redirected_to :action => "login" > > > post test_page, :user => user > > > assert_response :redirect > > > assert_redirected_to :action => protected_page > > > assert_nil session[:protected_page] > > > end > > > > def cookie_value(symbol) > > > cookies[symbol.to_s].value.first > > > end > > > > def cookie_expires(symbol) > > > cookies[symbol.to_s].expires > > > end > > > > thx. > > > > -karl--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---