# Alias\_method\_chain and ActiveSupport::TestCase

**URL:** <https://www.ruby-forum.com/t/alias-method-chain-and-activesupport-testcase/177670>\
**Category:** Rails\
**Created:** [October 21, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/alias-method-chain-and-activesupport-testcase/177670 "2009-10-21T20:40:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [October 21, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/alias-method-chain-and-activesupport-testcase/177670/1 "2009-10-21T20:40:16Z")

</div>

I’m trying to add some functionality to the setup method of all my  
tests.

I added a method called setup\_with\_ts in the test\_helper and chained  
it using alias\_method\_chain :setup, :ts and that works great for every  
test that does not define setup in the test.

So for my unit tests i tried also defining the same method  
setup\_with\_ts in a  
class ActiveSupport::TestCase  
def setup\_with\_ts  
puts “setup with ts”  
setup\_without\_ts  
end  
alias\_method\_chain :setup, :ts  
end

it was just a test to see what would happen.

if i called this method in the setup of a test like :  
def setup  
setup\_with\_ts  
.  
.  
.  
end

that will call it just fine so the method exists. but if i remove the  
setup\_with\_ts line and run a test the setup method in the test runs,  
but it never runs my aliased chained method.

i’m not sure why the alias\_method\_chain seems to work for test with no  
setup method and not for one that has a setup method defined. Any  
help would be great.

Thanks.  
Erik

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [October 21, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/alias-method-chain-and-activesupport-testcase/177670/2 "2009-10-21T20:51:23Z")

</div>

Just an update:

It is some kind of order of file loading or something.

If i put  
def setup\_with\_ts  
puts “setup with ts”  
setup\_without\_ts  
end  
alias\_method\_chain :setup, :ts

that directly into a test with a setup method it works fine, but not  
if i define it in the test\_helper.rb

Erik

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [October 21, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/alias-method-chain-and-activesupport-testcase/177670/3 "2009-10-21T21:02:31Z")

</div>

Another update:  
if i do:  
class ActiveSupport::TestCase  
def setup\_with\_ts  
puts “setup with ts”  
setup\_without\_ts  
end  
alias\_method\_chain :setup, :ts  
end

and then do puts self.methods in one of my tests it shows those  
methods exist. but they do not get called unless i define  
setup\_with\_ts and alias it in each test…

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [October 22, 2009, 9:41am UTC](https://www.ruby-forum.com/t/alias-method-chain-and-activesupport-testcase/177670/4 "2009-10-22T09:41:49Z")

</div>

On Oct 21, 7:38 pm, erik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i’m not sure why the alias\_method\_chain seems to work for test with no  
> setup method and not for one that has a setup method defined. Any  
> help would be great.

In a nutshell, because there is no reason it should. If a class  
overrides the superclass implementation of a method then that  
superclass implementation is not called unless the new implementation  
explicitly does it. You should be able to do

def setup  
super  
…  
end

However you don’t need to do all this, because ActiveSupport::TestCase  
already handles this sort of stuff. If you do

class ActiveSupport::TestCase  
def setup\_with\_ts  
end

setup :setup\_with\_ts  
end

then your method will get called at the appropriate time  
(ActiveSupport sets up a callback chain that is run just before setup  
is called)

Fred
