Problem with routes in functional testing
I'm making a simple test project to prepare myself for my test. I'm fairly new to nested resources, in my example I have a newsitem and each newsitem has comments.
The routing looks like this:
resources :comments
resources :newsitems do
resources :comments
end
I'm setting up the functional tests for comments at the moment and I ran into some problems.
This will get the index of the comments of a newsitem. @newsitem is declared in the setup ofc.
test "should get index" do
get :index,:newsitem_id => @newsitem
assert_response :success
assert_not_nil assigns(:newsitem)
end
But the problem lays here, in the "should get new".
test "should get new" do
get new_newsitem_comment_path(@newsitem)
assert_response :success
end
I'm getting the following error.
ActionController::RoutingError: No route matches {:controller=>"comments", :action=>"/newsitems/1/comments/new"}
But when I look into the routes table, I see this:
new_newsitem_comment GET /newsitems/:newsitem_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
Can't I use the开发者_开发知识库 name path or what I'm doing wrong here?
Thanks in advance.
The problem is in the way your test specifies the URL. The error message is:
No route matches {:controller=>"comments", :action=>"/newsitems/1/comments/new"}
and of course there is no action called "/newsitems/1/comments/new". You want to pass the hash { :controller => :comments, :action => :new, :news_item_id => 1 }
.
The right syntax is simply:
get :new, :news_item_id => 1
(Assuming Rails 3)
Try this in your routes.rb
GET 'newsitems/:newsitem_id/comments/new(.:format)' => 'comments#new', :as => :new_newsitem_comment
精彩评论