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