Ruby on rails 功能测试中的路由问题
我正在做一个简单的测试项目,为我的测试做准备。 我对嵌套资源相当陌生,在我的示例中,我有一个newsitem,每个newsitem都有注释 路由如下所示:Ruby on rails 功能测试中的路由问题,ruby-on-rails,resources,nested,functional-testing,nested-resources,Ruby On Rails,Resources,Nested,Functional Testing,Nested Resources,我正在做一个简单的测试项目,为我的测试做准备。 我对嵌套资源相当陌生,在我的示例中,我有一个newsitem,每个newsitem都有注释 路由如下所示: resources :comments resources :newsitems do resources :comments end 目前我正在为注释设置功能测试,遇到了一些问题 这将获得新闻项目评论的索引@新闻项在设置ofc中声明 test "should get index" do get :index,:newsi
resources :comments
resources :newsitems do
resources :comments
end
目前我正在为注释设置功能测试,遇到了一些问题
这将获得新闻项目评论的索引@新闻项在设置ofc中声明
test "should get index" do
get :index,:newsitem_id => @newsitem
assert_response :success
assert_not_nil assigns(:newsitem)
end
但问题就在这里,“应该变得新”
我得到以下错误
ActionController::RoutingError: No route matches {:controller=>"comments", :action=>"/newsitems/1/comments/new"}
但当我查看routes表时,我看到:
new_newsitem_comment GET /newsitems/:newsitem_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
我不能使用名称路径或我在这里做错了什么
提前感谢。(假设Rails 3)
在你的路线中试试这个。rb
GET 'newsitems/:newsitem_id/comments/new(.:format)' => 'comments#new', :as => :new_newsitem_comment
问题在于测试指定URL的方式。错误消息是:
No route matches {:controller=>"comments", :action=>"/newsitems/1/comments/new"}
当然,没有名为“/newsitems/1/comments/new”的操作。您想要传递散列{:controller=>:comments,:action=>:new,:news\u item\u id=>1}
正确的语法很简单:
get :new, :news_item_id => 1
还是一样的问题。我现在正在使用get:index、{:controller=>“/comments”、:newsitem_id=>@newsitem},它不会返回任何错误。但这只会让我进入主机:端口/评论/新建,对吗?但这不应该是一个问题,因为我给它的新闻项目的权利?我觉得很奇怪为什么我不能使用路径:如果你在
routes.rb中指定嵌套资源,我想你应该完全删除这一行。Rails应该为您设置一个命名路径。检查rake路由的输出
,但它应该类似于new\u newsitem\u comment
我在功能测试中遇到了类似的问题,我有许多嵌套路由。这帮了大忙!谢谢@zettetic
get :new, :news_item_id => 1