# How to test a function not a action on controller on ruby

**URL:** <https://www.ruby-forum.com/t/how-to-test-a-function-not-a-action-on-controller-on-ruby/118370>\
**Category:** Rails\
**Created:** [October 26, 2007, 6:56am UTC](https://www.ruby-forum.com/t/how-to-test-a-function-not-a-action-on-controller-on-ruby/118370 "2007-10-26T06:56:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [October 26, 2007, 6:56am UTC](https://www.ruby-forum.com/t/how-to-test-a-function-not-a-action-on-controller-on-ruby/118370/1 "2007-10-26T06:56:22Z")

</div>

Hi, everyone,  
How to test a function not a action on controller test on ruby? like  
this:  
def namesomeone ( firstname, lastname)  
some codes  
end  
how to test this function namesomeone.  
who knows, please tell me, Thanks!

---

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [October 26, 2007, 7:16am UTC](https://www.ruby-forum.com/t/how-to-test-a-function-not-a-action-on-controller-on-ruby/118370/2 "2007-10-26T07:16:49Z")

</div>

On 10/25/07, OnRails [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

I personally feel that if something is difficult to test then it’s  
probably not designed very well. This is almost certainly the case  
for you. Most likely you should move that method to the model, and it  
becomes obvious how to test it. Or you could write a test for an  
action that uses it, and verify that you get the proper output.

If a method isn’t part of an object’s API, you shouldn’t be writing a  
test for it! That’s far too granular. You want to write tests for an  
object’s observable behavior.

Pat

---

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [October 26, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/how-to-test-a-function-not-a-action-on-controller-on-ruby/118370/3 "2007-10-26T20:17:55Z")

</div>

There is nothing bad about having a function that is not an action in  
a controller. Filters are a perfect example of methods in controllers  
that aren’t actions. Testing these methods directly is a great idea  
as well. Since they’re usually protected, you’ll have to use .send  
which will work for non-protected methods as well:

@controller.send(:method, \*args)

or in Rspec:

controller.send(:method, \*args)
