Overriding get, post, etc. in functional tests (i.e. Test::U

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 T. 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:


Cheers,

  • Jacob A.

Jacob A. 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.


Phlip
http://www.oreilly.com/catalog/9780596510657/
^ assert_xpath
O'Reilly Media - Technology and Business Training ← assert_raise_message

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