Rails 2.3.8(使用Mocha测试)
在我的routes.rb文件中:
map.root(:controller =>'application', :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
我意识到存根'返回'子句不正确,但我没有 到了那里:
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
答案 0 :(得分:1)
如果您的目标是测试/
的请求是否一致地映射,则需要使用集成测试。在rails的功能测试中,名为helper的各种http方法接受操作名称作为其参数,而不是URI。