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

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 :slight_smile:

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 :slight_smile:

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.

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