ruby-on-rails - 功能测试中的路由问题

标签 ruby-on-rails resources nested functional-testing nested-resources

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

路由看起来像这样:

resources :comments

resources :newsitems do
    resources :comments
end

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

这将获取新闻站点评论的索引。 @newsitem 在 setup 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"}

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

提前致谢。

最佳答案

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

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

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

正确的语法很简单:

get :new, :news_item_id => 1

关于ruby-on-rails - 功能测试中的路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4615297/

相关文章:

javascript - CryptoJS 不适用于 IV 和 key ,但它适用于 Ruby

ruby-on-rails - 以 rails 形式在循环内嵌套属性的字段

python - 在 Finalize 类型调用后使 Python 对象无法使用

perl - 我在哪里可以得到关于 Perl 相关问题的好答案?

c++ - 有关层次结构的一般建议

mysql 在更新中嵌套选择

python - 将嵌套列表拆分为两个列表

ruby-on-rails - 来自应用程序 : cannot load such file -- js_regex (LoadError) with client_side_validations 的消息

javascript - geolocation.getCurrentPosition 晚上失败 -- 如何提供自己的 api key ?

javascript - Rails 在 restful/resourceful Controller 中的 javascript 模板的最佳实践是什么?