测试 Rails 路线:找不到 ActionController::Assertions::RoutingAssertions 方法
我正在尝试在 Rails 2.3.4 应用程序上测试路线。有几个网站解释了如何测试路线,包括 Rails 文档,但我按照说明遇到错误。
首先,我假设这些测试可以在相关的单元测试文件中完成。似乎没有更明显的地方,并且没有文档指定。
也就是说,这是 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 应用程序中重新创建该场景,也会出现此问题。我认为我不应该将这些模块包含在我的测试用例中。这里可能出了什么问题?
I'm trying to test the routes on my rails 2.3.4 application. There are several sites that explain how to test routes, including the rails docs, but I'm getting errors following the instructions.
First, I'm assuming that these tests can be done in related unit test files. There seems to be no more obvious place, and none of the docs specify.
That said, this is a condensed version of 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
fails with the error:
NoMethodError: undefined method `assert_routing' for #<TitleTest:0x7f387232ec98>
/test/unit/title_test.rb:7:in `test_routes'
I saw in the Rails API that assert_routing is defined in ActionController::Assertions::RoutingAssertions
, so I attempted to include that module, only to have it fail elsewhere.
Note the commented include
line in the code example above.
NoMethodError: undefined method `clean_backtrace' for #<TitleTest:0x7fd895fadf00>
/test/unit/title_test.rb:7:in `test_routes'
clean_backtrace
is another testing method defined in ActionController::TestCase::Assertions.
I'm not getting any google search results for these errors - no one else seems to be having this problem. The problem also occurs if I recreate the scenario in a freshly generated rails app. I don't think I should be having to include these modules in my test cases. What might be wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Giant 上有一个升级 Rails 陷阱的列表机器人。显然,您的路由测试应该使用类
ActionController::TestCase
,而不是ActiveSupport::TestCase
。There's a list of upgrading Rails gotchas on Giant Robots. Apparently your routing test should use class
ActionController::TestCase
, notActiveSupport::TestCase
.路由测试应该作为集成测试的一部分来完成。
这些可以使用
script/generate script/generateintegration_test paths
生成,例如:
Routing tests should be done as part of integration tests.
These can be generated using
script/generate script/generate integration_test routes
An example:
从 Rails 2.3.2 开始,您的路由单元测试文件必须包含
ActionController::Assertions::RoutingAssertions
模块。Your routing unit test file has to include the
ActionController::Assertions::RoutingAssertions
module since Rails 2.3.2.