【问题标题】:Rails 2.3.8: How to functionally test the root route by requesting '/'?Rails 2.3.8:如何通过请求“/”来功能测试根路由?
【发布时间】: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


【解决方案1】:

如果您的目标是测试对 / 的请求是否一致映射,则您需要改用集成测试。在 rails 的功能测试中,各种名为 helper 的 http 方法接受操作名称作为参数,而不是 URI。

Helpers available to functional tests

Code for the above

Integration test helpers

另外,assert_routing

【讨论】:

  • assert_routing 很可爱。它验证对“/”的请求应该到达render_404_not_found 方法。够好了。现在我想测试该方法是否真正被调用。不幸的是,该方法进行了渲染(参见pastie.org/1526021),因此 ISTM 最干净的路径是将其存根以给出不同的结果。不幸的是,渲染是一种“副作用”操作,而不是简单的方法返回值,我还没有弄清楚如何存根。
  • 这可能是基本的 Ruby;设置一个在执行时执行备用渲染代码的存根。不幸的是,到目前为止,我的所有尝试都导致代码在定义时间执行。有没有一种方法可以声明一个 Proc 以便“将此块视为文字,直到使用 #call 调用”? (使它成为一个独特的-闭包,是吗?)
  • 老实说,我现在只是创建一个集成测试,运行 {get,post,head,funk} root_path 并使用模板渲染断言检查结果。毫无疑问,我又错过了重点。
  • 不,渲染断言可能是解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多