How to write functional tests for a nested route?

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 |,
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.


$ rake test

fails the functional tests for the comments controller, not
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
    “one” and “two” to “post_one” and “post_two”, respectively.
  2. Edit test/fixtures/comments.yml so it looks like:
    post: post_one
    body: MyText

post: post_two
body: MyText

  1. Edit test/functional/comments_controller_test.rb and change all of
    tests from something like:

test “should update comment” do
put :update, :id => comments(:one).id, :comment => { }
assert_redirected_to comment_path(assigns(:comment))

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))

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
new post id. This all seems pretty straightforward to me now (having
the better part of the day figuring it out). But I am curious… what
folks who know what they are doing do in this case? (Ok, perhaps all of
use Rspec, or something else… perhaps I should narrow my question to
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
up the “post_id” for my functional tests?