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

**URL:** <https://www.ruby-forum.com/t/overriding-get-post-etc-in-functional-tests-i-e-test-u/110359>\
**Category:** Rails\
**Created:** [July 25, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/overriding-get-post-etc-in-functional-tests-i-e-test-u/110359 "2007-07-25T19:12:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [July 25, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/overriding-get-post-etc-in-functional-tests-i-e-test-u/110359/1 "2007-07-25T19:12:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [July 25, 2007, 7:29pm UTC](https://www.ruby-forum.com/t/overriding-get-post-etc-in-functional-tests-i-e-test-u/110359/2 "2007-07-25T19:29:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [July 25, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/overriding-get-post-etc-in-functional-tests-i-e-test-u/110359/3 "2007-07-25T20:27:41Z")

</div>

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/](http://www.oreilly.com/catalog/9780596510657/)  
^ assert\_xpath  
[O'Reilly Media - Technology and Business Training](http://tinyurl.com/23tlu5) ← assert\_raise\_message

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [July 26, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/overriding-get-post-etc-in-functional-tests-i-e-test-u/110359/4 "2007-07-26T14:33:18Z")

</div>

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
