gpt4 book ai didi

ruby-on-rails - rails 2.3.8 : How to functionally test the root route by requesting '/' ?

转载 作者:行者123 更新时间:2023-11-28 20:47:01 25 4
gpt4 key购买 nike

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

我意识到 stub “返回”子句不正确,但我没有到那里了:

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

最佳答案

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

Helpers available to functional tests

Code for the above

Integration test helpers

此外,assert_routing

关于ruby-on-rails - rails 2.3.8 : How to functionally test the root route by requesting '/' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4889651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com