Topic: How do you functional test nested resources?

I can't figure out the syntax for getting the id of the parent resource, so all the tests error with "could not find Parent without id"

the controller says
@parent = Parent.find(params[:parent_id])

how do you get the id into the test?

Re: How do you functional test nested resources?

You can pass it along as a parameter:

# in test, might want "post" instead
get :some_action, :parent_id => 1

Railscasts - Free Ruby on Rails Screencasts

Re: How do you functional test nested resources?

i have a similar problem closely related - before_filter calls a method with the following code within my parent_controller:

@parent = Parent.find(params[:parent_id])
@group = Group.find(@parent.group_id)

i get the exception of not being able to find Group "without an id"..

my rails app works fine but it keeps failing on this functional test.  i tried including the Group model, but it didnt solve the problem..

i find this odd, since group is being created after the instance variable parent is created..

Last edited by d3wu (2007-03-03 04:44:41)

Re: How do you functional test nested resources?

Did you set the group_id column in the parent fixtures?

Also, there's no need to fetch a group this way, you can do it like this:

@parent = Parent.find(params[:parent_id])
@group = @parent.group

That is, if you set up the has_many/belongs_to association in the models.

Railscasts - Free Ruby on Rails Screencasts

Re: How do you functional test nested resources?

ah that was the problem - forgot to add the id's to the fixtures

thanks for the tip on how to fetch the group - i didn't realize i get that method after having the belongs_to association for it