Forum: Ruby on Rails Overriding get, post, etc. in functional tests (i.e. Test::U

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John T. (Guest)
on 2007-07-25 21:12
(Received via mailing list)
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
Jacob A. (Guest)
on 2007-07-25 21:29
(Received via mailing list)
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 :-)

--
Cheers,
- Jacob A.
Phlip (Guest)
on 2007-07-25 22:27
(Received via mailing list)
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 :-)

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
John T. (Guest)
on 2007-07-26 16:33
(Received via mailing list)
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
This topic is locked and can not be replied to.