Where is wrong with contoller test?

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!

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.

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!

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!