# Controller utility method: want to use in tests. Where should it go?

**URL:** <https://www.ruby-forum.com/t/controller-utility-method-want-to-use-in-tests-where-should-it-go/159912>\
**Category:** Rails\
**Created:** [February 10, 2009, 1:01am UTC](https://www.ruby-forum.com/t/controller-utility-method-want-to-use-in-tests-where-should-it-go/159912 "2009-02-10T01:01:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![itsastickup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7feea3/32.png) [@itsastickup](https://www.ruby-forum.com/u/itsastickup)\
**Post date:** [February 10, 2009, 1:01am UTC](https://www.ruby-forum.com/t/controller-utility-method-want-to-use-in-tests-where-should-it-go/159912/1 "2009-02-10T01:01:55Z")

</div>

Hi,

I’ve a simple method I call in my action methods. So far I’ve put it  
in application.rb in the ActionController class.

However I can’t call it from my test code. Where should I be putting  
it? I’ve tried adding this to application.rb :

module ActionController

def handy  
return dostuff  
end

end

but it didn’t work.

Is this possible?

---

<div class="post-metadata">

**Author:** ![itsastickup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7feea3/32.png) [@itsastickup](https://www.ruby-forum.com/u/itsastickup)\
**Post date:** [February 10, 2009, 1:12am UTC](https://www.ruby-forum.com/t/controller-utility-method-want-to-use-in-tests-where-should-it-go/159912/2 "2009-02-10T01:12:28Z")

</div>

On 10 Feb 2009, at 00:01, itsastickup wrote:

> def handy  
> return dostuff  
> end
> 
> end
> 
> but it didn’t work.
> 
> Is this possible?

adding a method to application.rb lets controllers call it because you  
are adding a method to ApplicationController and your controllers  
inherit from that.  
In a test you are subclassing Test::Unit::TestCase (or more recently  
ActiveSupport::TestCase or some other class if you are using a  
different testing framework), so clearly you can’t call and  
ApplicationController instance method. Easiest thing is probably to  
put the methods in a module and include it in both places.

Fred

---

<div class="post-metadata">

**Author:** ![itsastickup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7feea3/32.png) [@itsastickup](https://www.ruby-forum.com/u/itsastickup)\
**Post date:** [February 10, 2009, 1:21am UTC](https://www.ruby-forum.com/t/controller-utility-method-want-to-use-in-tests-where-should-it-go/159912/3 "2009-02-10T01:21:50Z")

</div>

Ok thanks
