2

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.

Wishmaster
  • 21
  • 2

2 Answers2

7

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
zetetic
  • 47,184
  • 10
  • 111
  • 119
  • I was having a similar issue with functional testing where I had many nested routes. This helped immensely! Thanks @zetetic – Don Leatham Nov 28 '11 at 06:00
0

(Assuming Rails 3)

Try this in your routes.rb

GET    'newsitems/:newsitem_id/comments/new(.:format)' => 'comments#new', :as => :new_newsitem_comment
David Sulc
  • 25,946
  • 3
  • 52
  • 54
  • Still the same problemen. I'm using get :index,{:controller => "/comments",:newsitem_id => @newsitem} now and it doesnt return any errors. But this will just get me to the host:port/comments/new right? But it shouldn't be a problem since I give the newsitem with it right? Just find it so strange why I cant use the paths :s – Wishmaster Jan 06 '11 at 14:12
  • Given you actually specify nested resources in `routes.rb`, I think you should remove that line completely. Rails should have set up a named path for you. Check the output of `rake routes`, but it should be something like `new_newsitem_comment` – David Sulc Jan 06 '11 at 14:38