【问题标题】:Rails 3 integration test mangles (model)_path(@instance) helperRails 3 集成测试 mangles (model)_path(@instance) 助手
【发布时间】:2011-08-31 19:13:37
【问题描述】:

我有一个 Doc 类,其中包含由脚手架生成器创建的标准路由。在代码中,我使用

docs_path(@doc)  # => /docs/7

效果很好。

在我的集成测试中我使用:

get  docs_path(@doc) #=> /docs.7

这根本不起作用。

这对于使用 rails 模型的标准路由的其他控制器是相同的。

使用:

get "/docs/7" 

在集成测试代码中工作正常,但是当我尝试使用“_path”或“_url”帮助程序处理页面时,它会以类似的方式破坏它们,从而导致错误。

注意:当我在开发环境中打开笔记本电脑上的实际页面时,此代码可以正常工作。

:当我尝试通过编写测试来做正确的事情并且测试引入了正常环境中不存在的错误时,这非常令人沮丧。你是怎么处理的???

使用轨道 3.0.8。

以下是 rake.routes 中的相关行

docs        GET      /docs(.:format)          {:action=>"index", :controller=>"docs"}
            POST     /docs(.:format)          {:action=>"create", :controller=>"docs"}
new_doc     GET      /docs/new(.:format)      {:action=>"new", :controller=>"docs"}
edit_doc    GET      /docs/:id/edit(.:format) {:action=>"edit", :controller=>"docs"}
doc         GET      /docs/:id(.:format)      {:action=>"show", :controller=>"docs"}
            PUT      /docs/:id(.:format)      {:action=>"update", :controller=>"docs"}
            DELETE   /docs/:id(.:format)      {:action=>"destroy", :controller=>"docs"}

为什么要这样做?我可以修复它吗?

【问题讨论】:

    标签: ruby-on-rails-3 routes integration-testing helper


    【解决方案1】:

    “标准”路由是一个单数命名路由:doc_path。您正在使用docs_path。当您想要链接到特定文档时使用单数名称,当您想要获取这些文档的列表时使用复数名称。

    【讨论】:

      猜你喜欢
      • 2014-09-05
      • 2011-11-26
      • 2014-04-18
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 2010-10-01
      相关资源
      最近更新 更多