2

我正在制作一个简单的测试项目来为我的测试做准备。我对嵌套资源相当陌生,在我的示例中,我有一个 newsitem,每个 newsitem 都有评论。

路由看起来像这样:

resources :comments

resources :newsitems do
    resources :comments
end

我目前正在为评论设置功能测试,但遇到了一些问题。

这将获得新闻站点评论的索引。@newsitem 在 ofc 的设置中声明。

test "should get index" do
    get :index,:newsitem_id => @newsitem
    assert_response :success
    assert_not_nil assigns(:newsitem)
end

但问题出在这里,在“应该得到新的”。

 test "should get new" do
    get new_newsitem_comment_path(@newsitem)
    assert_response :success
 end

我收到以下错误。

ActionController::RoutingError: No route matches {:controller=>"comments", :action=>"/newsitems/1/comments/new"}

但是当我查看路由表时,我看到了:

new_newsitem_comment GET    /newsitems/:newsitem_id/comments/new(.:format)      {:action=>"new", :controller=>"comments"}

我不能使用名称路径或我在这里做错了什么吗?

提前致谢。

4

2 回答 2

7

问题在于您的测试指定 URL 的方式。错误信息是:

No route matches {:controller=>"comments", :action=>"/newsitems/1/comments/new"}

当然,没有名为“/newsitems/1/comments/new”的操作。你想传递 hash { :controller => :comments, :action => :new, :news_item_id => 1 }

正确的语法很简单:

get :new, :news_item_id => 1
于 2011-01-06T20:18:27.160 回答
0

(假设 Rails 3)

试试这个routes.rb

GET    'newsitems/:newsitem_id/comments/new(.:format)' => 'comments#new', :as => :new_newsitem_comment
于 2011-01-06T13:49:03.207 回答