功能测试中的路线问题
我正在制作一个简单的测试项目来为测试做好准备。 我对嵌套资源相当陌生,在我的示例中,我有一个新闻站点,每个新闻站点都有评论。
路由看起来像这样:
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"}
我不能使用名称路径还是我在这里做错了什么?
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于您的测试指定 URL 的方式。错误消息是:
当然没有名为“/newsitems/1/comments/new”的操作。您想要传递哈希
{ :controller =>; :评论,:行动=> :新,:news_item_id => 1 }
。正确的语法很简单:
The problem is in the way your test specifies the URL. The error message is:
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:
(假设 Rails 3)
在你的
routes.rb
中尝试一下(Assuming Rails 3)
Try this in your
routes.rb