Ruby on rails 测试轨道路线:can';找不到ActionController::断言::RoutingAssertions方法
我正在尝试在rails 2.3.4应用程序上测试路由。有几个站点解释了如何测试路由,包括rails文档,但我在遵循说明时遇到了错误 首先,我假设这些测试可以在相关的单元测试文件中完成。似乎没有比这更明显的地方了,也没有一个文档具体说明了这一点 这就是说,这是Ruby on rails 测试轨道路线:can';找不到ActionController::断言::RoutingAssertions方法,ruby-on-rails,unit-testing,routes,integration-testing,Ruby On Rails,Unit Testing,Routes,Integration Testing,我正在尝试在rails 2.3.4应用程序上测试路由。有几个站点解释了如何测试路由,包括rails文档,但我在遵循说明时遇到了错误 首先,我假设这些测试可以在相关的单元测试文件中完成。似乎没有比这更明显的地方了,也没有一个文档具体说明了这一点 这就是说,这是test/unit/titletest.rb require File.dirname(__FILE__) + '/../test_helper' class TitleTest < Test::Unit::TestCase #
test/unit/titletest.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
我在Rails API中看到assert_路由是在ActionController::Assertions::RoutingAssertions
中定义的,因此我尝试包含该模块,但在其他地方失败了
注意上面代码示例中注释的include
行
NoMethodError: undefined method `clean_backtrace' for #<TitleTest:0x7fd895fadf00>
/test/unit/title_test.rb:7:in `test_routes'
NoMethodError:for的未定义方法“clean\u backtrace”#
/测试/单元/标题测试。rb:7:在“测试路线”中
clean_backtrace
是ActionController::TestCase::Assertions中定义的另一种测试方法
我没有收到任何关于这些错误的谷歌搜索结果——似乎没有其他人有这个问题。如果在新生成的rails应用程序中重新创建场景,也会出现问题。我认为我不应该在测试用例中包含这些模块。这里可能有什么问题?自Rails 2.3.2以来,路由单元测试文件必须包括
ActionController::Assertions::RoutingAssertions
模块。路由测试应作为集成测试的一部分进行
可以使用script/generate script/generate integration\u测试路径生成这些脚本
例如:
class RoutesTest < ActionController::IntegrationTest
fixtures :all
def test_resources_route
assert_routing "titles/15", { :controller => "titles", :action => "show", :id => "15" }
end
end
class RouteTest“titles”,:action=>“show”,:id=>“15”}
结束
结束
有一个关于巨型机器人的列表。显然,您的路由测试应该使用类ActionController::TestCase
,而不是ActiveSupport::TestCase
您是否尝试过包含ActionController::TestCase::断言?在包含ActionController::TestCase::断言之后,它似乎可以工作。我不认为我应该添加这些包含是正常的。我也尝试从ActiveSupport::TestCase继承,而不是从Test::Unit::TestCase继承,但我仍然需要手动包含。这在Rails 3中似乎也起作用,不幸的是,指南中也省略了这些信息。对于任何想测试铁路线路的人来说,这实际上是一个非常棒的答案
class RoutesTest < ActionController::IntegrationTest
fixtures :all
def test_resources_route
assert_routing "titles/15", { :controller => "titles", :action => "show", :id => "15" }
end
end