# How to write functional tests for a nested route?

**URL:** <https://www.ruby-forum.com/t/how-to-write-functional-tests-for-a-nested-route/155261>\
**Category:** Rails\
**Created:** [December 9, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/how-to-write-functional-tests-for-a-nested-route/155261 "2008-12-09T22:30:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 9, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/how-to-write-functional-tests-for-a-nested-route/155261/1 "2008-12-09T22:30:22Z")

</div>

Starting from here:

$ rails classic  
$ cd classic  
$ script/generate scaffold post title:string body:text  
$ rake db:migrate  
$ rake test  
(tests pass, as expected)  
$ script/generate scaffold comment post:belongs\_to body\_text  
$ rake db:migrate  
$ rake test  
(tests pass again, as expected)

Edit config/routes.rb, app/controllers/comments\_controller.rb,  
app/views/comments/\*, etc… as described in  
[Rolling with Rails 2.0 - The First Full Tutorial - Part 1 | AkitaOnRails.com](http://www.akitaonrails.com/2007/12/12/rolling-with-rails-2-0-the-first-full-tutorial)(and,  
doubtless, other places) to nest the routes to the comments controller  
within the routes to the posts controller. The key change being that  
config/routes.rb now looks like:

map.resources :posts, :has\_many =\> :comments

with no other routes defined.

Now,

$ rake test

fails the functional tests for the comments controller, not  
unexpectedly,  
because the routes have all changed.

How to folks typically rewrite the functional tests in this case?  
The answer I implemented was:

1. Edit test/fixtures/posts.yml and change the name of the two posts  
from  
“one” and “two” to “post\_one” and “post\_two”, respectively.
2. Edit test/fixtures/comments.yml so it looks like:  
one:  
post: post\_one  
body: MyText

two:  
post: post\_two  
body: MyText

1. Edit test/functional/comments\_controller\_test.rb and change all of  
the  
tests from something like:

test “should update comment” do  
put :update, :id =\> comments(:one).id, :comment =\> { }  
assert\_redirected\_to comment\_path(assigns(:comment))  
end

to something like:

test “should update comment” do  
post\_id = posts(:post\_one)  
put :update, :post\_id =\> post\_id, :id =\> comments(:one).id, :comment  
=\>  
{ }  
assert\_redirected\_to post\_comment\_path(post\_id, assigns(:comment))  
end

In other words, I placed the following line:

```
post_id = posts(:post_one)

```

at the beginning of each test, changed the get/put/post/delete call to  
include the post\_id, and changed all of the redirected\_to tests to use  
the  
new post id. This all seems pretty straightforward to me now (having  
spent  
the better part of the day figuring it out). But I am curious… what  
do  
folks who know what they are doing do in this case? (Ok, perhaps all of  
you  
use Rspec, or something else… perhaps I should narrow my question to  
"What  
do folks who run functional tests according to the best practices  
implied by  
the default scaffold do in this case?

Also, is there anything akin to a “before\_filter” that I could run to  
load  
up the “post\_id” for my functional tests?

–wpd
