John Trupiano
2007-Jul-25 17:11 UTC
Overriding get, post, etc. in functional tests (i.e. Test::Unit::TestCase)
Hey guys, Can someone point me in the right direction here? I''m sure others have had similar scenarios. Basically, I have login-protected areas of my application (e.g. an admin section). I store a session value to identify the particular user is logged in. Basically, I want to append a single key-value pair (logged_in_user => ''jim'') to the session on every call to get, post, put, delete, xhr, etc. within my functional tests. I''m not even sure that these are actual methods-- I''ve tried overriding the methods as such-- class AdminTestCase < Test::Unit::TestCase def admin_session {:logged_in_administrator => 1} end [:get, :post, :put, :delete].each do |meth| src = <<-END_OF_SRC def #{meth.to_s}(request_method, action, parameters = nil, session = {}, flash = nil) super(request_method, action, parameters, session.merge(admin_session), flash) end END_OF_SRC class_eval src, __FILE__, __LINE__ end end My calls to get, post, etc. appear to merely invoke the previously defined function (or whatever it may be). Has anyone tried to do this before? In the Ruby code /lib/test/unit/ testcase.rb, these methods (as expected) do not exist. Furthermore, there does not appear to be an implementation of method_missing. This leads me to suspect that this behavior/functionality is defined somewhere in Rails.....but I''m having a lot of trouble finding it. Thanks in advance! -John --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jacob Atzen
2007-Jul-25 17:28 UTC
Re: Overriding get, post, etc. in functional tests (i.e. Test::Unit::TestCase)
John Trupiano wrote:> Hey guys, > > Can someone point me in the right direction here? I''m sure others > have had similar scenarios. Basically, I have login-protected areas > of my application (e.g. an admin section). I store a session value to > identify the particular user is logged in. Basically, I want to > append a single key-value pair (logged_in_user => ''jim'') to the > session on every call to get, post, put, delete, xhr, etc. within my > functional tests.I took a simpler approach and simply added admin_get, admin_post, etc. to the test_helper. Works for me even if I have to put admin_ in front of many calls. I''m sure it can be refined further, but for now it works for me :-) -- Cheers, - Jacob Atzen --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Phlip
2007-Jul-25 18:26 UTC
Re: Overriding get, post, etc. in functional tests (i.e. Test::Unit::TestCase)
Jacob Atzen wrote:> I took a simpler approach and simply added admin_get, admin_post, etc. > to the test_helper. Works for me even if I have to put admin_ in front > of many calls. I''m sure it can be refined further, but for now it works > for me :-)Been there done that. Override get (and post) by writing a new get, and make it call super. Put it in a class that derives from Test::Unit::TestCase. super will now chain to the parent class get. Derive every test suite that needs the augmented get from your new class, not Test::Unit::TestCase. And make sure all your test suites are coherent; they should only test things that need the same fixtures, setup, and overrides. That means that only suites which need the new improved get should derive from its class. -- Phlip http://www.oreilly.com/catalog/9780596510657/ ^ assert_xpath http://tinyurl.com/23tlu5 <-- assert_raise_message --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
John Trupiano
2007-Jul-26 12:07 UTC
Re: Overriding get, post, etc. in functional tests (i.e. Test::Unit::TestCase)
Thanks for the responses guys. Jacob-- I was trying to avoid having to edit all of my currently existing functional tests (I added authentication at the very end of development). Phlip-- Did you see my code snippet I included? I was trying to do exactly that-- do you see something wrong with that? My class AdminTestCase is defined at the bottom of test_helper (outside of the Test::Unit::TestCase class definition). Anyone else have any thoughts/suggestions? Thanks! -John On Jul 25, 2:26 pm, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Jacob Atzen wrote: > > I took a simpler approach and simply added admin_get, admin_post, etc. > > to the test_helper. Works for me even if I have to put admin_ in front > > of many calls. I''m sure it can be refined further, but for now it works > > for me :-) > > Been there done that. Override get (and post) by writing a new get, > and make it call super. Put it in a class that derives from > Test::Unit::TestCase. super will now chain to the parent class get. > > Derive every test suite that needs the augmented get from your new > class, not Test::Unit::TestCase. And make sure all your test suites > are coherent; they should only test things that need the same > fixtures, setup, and overrides. That means that only suites which need > the new improved get should derive from its class. > > -- > Phlip > http://www.oreilly.com/catalog/9780596510657/ > ^ assert_xpath > http://tinyurl.com/23tlu5 <-- assert_raise_message--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---