# Where is wrong with contoller test?

**URL:** <https://www.ruby-forum.com/t/where-is-wrong-with-contoller-test/114576>\
**Category:** Ruby\
**Created:** [September 16, 2007, 2:51am UTC](https://www.ruby-forum.com/t/where-is-wrong-with-contoller-test/114576 "2007-09-16T02:51:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@zlai](https://www.ruby-forum.com/u/zlai)\
**Post date:** [September 16, 2007, 2:51am UTC](https://www.ruby-forum.com/t/where-is-wrong-with-contoller-test/114576/1 "2007-09-16T02:51:58Z")

</div>

Hi, everybody!  
I got a strange question when I did a controller test as below  
On controller.rb:

def send\_welcome\_message(person)  
some codes;  
end  
there ‘person’ is instance variables.

the controller\_test.rb:  
def test\_send\_welcome\_message  
post :send\_welcome\_message, :person=\>{:id=\>‘2’,  
:name=\>‘somebody’}  
assert\_response :success  
end

when I run this test, There always said,

1. Error:  
test\_send\_welcome\_message(TestControllerTest):  
ArgumentError: wrong number of arguments (0 for 1)  
I know there maybe params are set wrong,  
but how can I fix it out? please help, Thanks!

---

<div class="post-metadata">

**Author:** ![zlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@zlai](https://www.ruby-forum.com/u/zlai)\
**Post date:** [September 17, 2007, 10:52am UTC](https://www.ruby-forum.com/t/where-is-wrong-with-contoller-test/114576/2 "2007-09-17T10:52:45Z")

</div>

OnRails R. wrote:

> Hi, everybody!  
> I got a strange question when I did a controller test as below  
> On controller.rb:
> 
> def send\_welcome\_message(person)  
> some codes;  
> end  
> there ‘person’ is instance variables.

Can I just check how you’re using this method.  
Is it a controller “action”, as in, do you access it directly from a  
url?  
eg. /controller/send\_welcome\_message/1

Or is it just a convenience method?

If it is an action, then an action can only be called without an  
argument  
(it takes input from the http params, and inherits any instance  
variables defined in before\_filters)

you say “person” is an instance variable.  
so it should look like this

def send\_welcome\_message  
do the code involving @person  
end

before\_filter :get\_person  
def get\_person  
@person = Person.find(params[:id])  
end

hopefully that’ll fix your problem.

---

<div class="post-metadata">

**Author:** ![zlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@zlai](https://www.ruby-forum.com/u/zlai)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/where-is-wrong-with-contoller-test/114576/3 "2007-09-17T13:24:59Z")

</div>

OnRails R. wrote:

> I got a strange question when I did a controller test as below

The best place for Rails question is the Google Group called “Ruby on  
Rails:  
Talk”. And you had better get at least the book /Agile Web D.  
on  
Rails/ before programming any more.

> On controller.rb:
> 
> def send\_welcome\_message(person)  
> some codes;  
> end  
> there ‘person’ is instance variables.

It is not; it’s a method parameter.

However, actions don’t (typically) take arguments. They always get data  
from  
their web pages from the params hash, so you need to use

person = params[:person]

> ArgumentError: wrong number of arguments (0 for 1)

Right; the test rig did not pass person where you think it did, but the  
action was expecting it there. Take it out of the action’s arguments.

BTW congrats for testing; it will make all this stuff easier as you go!

---

<div class="post-metadata">

**Author:** ![zlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@zlai](https://www.ruby-forum.com/u/zlai)\
**Post date:** [September 17, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/where-is-wrong-with-contoller-test/114576/4 "2007-09-17T20:21:30Z")

</div>

Thanks a lots, matthewrudy and Phlip  
I had posted this question on groups google. and got the same answer.

I think that is a function like “def send\_welcome\_message(person)”,  
and that is a model method(action) like “def send\_welcome\_message”,  
so when I defined “def send\_welcome\_message(person)”, I can call  
it in I method(action), then you don’t need to test this function.  
This idea is right or not?  
Thanks help!
