【发布时间】:2011-02-03 17:46:39
【问题描述】:
Rails 2.3.8(使用 Mocha 进行测试)
在我的 routes.rb 文件中:
map.root(:controller => '应用程序', :action => 'render_404_not_found')
在我的功能测试中,我想验证对“/”的请求 会妥善处理:
@controller.stubs(:render_404_not_found).returns(666)
%w( HEAD GET POST PUT DELETE ).each do |method|
@request = ActionController::TestRequest.new
@request.env['REQUEST_URI'] = uri
@request.path = uri
process(uri, nil, nil, nil, method)
assert_response(666,
"Request for '#{method} #{uri}'" +
'should have 404ed (or 666ed)')
end
我意识到存根“returns”子句不正确,但我没有 到了那里:
test_bogus_uri_path(EdgeConditionsTest):
ActionController::RoutingError: /usr/lib/ruby/gems/1.8/gems/ \
actionpack-2.3.8/lib /action_controller/routing/route_set.rb:420: \
in `generate': No route matches {:controller=>"application", :action=>"/"}
那么我如何测试这条路线是否正常工作并调用#render_404_not_found ?
谢谢!
基于@noodl 优秀建议的最终解决方案:
uri = '/'
%w( HEAD GET POST PUT DELETE ).each do |method|
assert_routing({
:method => method.downcase,
:path => uri
},
{
:controller => 'application',
:action => 'render_404_not_found'
},
{},
{},
"'#{method} #{uri}': " +
'Root route #failed')
end
#
# Now, ensure that our function is actually getting called..
#
%w( HEAD GET POST PUT DELETE ).each do |method|
send(method.downcase.to_sym, uri)
assert_response(404,
"Request for '#{method} #{uri}'" +
'should have 404ed')
assert_template('404.html')
end
【问题讨论】:
-
我认为您在尝试路由到 ApplicationController 时可能会遇到问题。我不认为它处理请求。
-
实际上,该路由工作正常(我的
application_controller.rb具有必要的方法,它按预期被调用)。问题在于编写测试以确保它保持正常工作.. -
您的问题不包括 uri 局部变量的定义(或功能测试定义的其余部分)。我知道你已经够大胡子了,不会搞砸的,但我看不出还有什么问题。再说一次,我从未见过在这种测试中使用过 process()。
-
@noodl 上面有一个
uri = '/'。不知何故,路径正在变成动作,因此不匹配根路由。 :-( 我正在使用process,因为替代方案是混乱的eval("#{method.downcase}(uri)") -
对不起,我真的没有帮助。你能指出我在你的测试中调用 process() 方法的定义吗?我很感兴趣,如果一如既往地无用的话。
标签: ruby-on-rails testing routing routes