我正在尝试测试我的rails 2.3.4应用程序上的路由。有几个站点解释了如何测试路由,包括rails docs,但是我按照说明收到了错误。

首先,我假设这些测试可以在相关的单元测试文件中完成。似乎没有更明显的地方,也没有文件说明。

那就是说,这是 test / unit / TitlesTest.rb

的浓缩版本
require File.dirname(__FILE__) + '/../test_helper'

class TitleTest < Test::Unit::TestCase
  # include ActionController::Assertions::RoutingAssertions

  def test_routes
    assert_routing "games", { :controller => "titles", :section => "games", :action => "index", :id => nil }
  end
end

rake test:units 失败并显示错误:

NoMethodError: undefined method `assert_routing' for #<TitleTest:0x7f387232ec98>
    /test/unit/title_test.rb:7:in `test_routes'

我在Rails API中看到assert_routing是在 ActionController :: Assertions :: RoutingAssertions 中定义的,所以我试图包含该模块,只是让它在其他地方失败。

请注意上面代码示例中注释的 include 行。

NoMethodError: undefined method `clean_backtrace' for #<TitleTest:0x7fd895fadf00>
    /test/unit/title_test.rb:7:in `test_routes'

clean_backtrace 是ActionController :: TestCase :: Assertions中定义的另一种测试方法。

我没有得到任何谷歌搜索结果的这些错误 - 没有其他人似乎有这个问题。如果我在新生成的rails应用程序中重新创建场景,也会出现问题。我不认为我应该在我的测试用例中包含这些模块。这可能有什么问题?

有帮助吗?

解决方案

升级Rails陷阱关于巨型机器人。显然你的路由测试应该使用class ActionController :: TestCase ,而不是 ActiveSupport :: TestCase

其他提示

路由测试应该作为集成测试的一部分进行。

这些可以使用 script / generate script / generate integration_test routes

生成

一个例子:

class RoutesTest < ActionController::IntegrationTest
  fixtures :all

  def test_resources_route
    assert_routing "titles/15", { :controller => "titles", :action => "show", :id => "15" }
  end
end

自Rails 2.3.2起,您的路由单元测试文件必须包含 ActionController :: Assertions :: RoutingAssertions 模块。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top