微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 任意或自定义URL的Rails功能测试

我的Rails应用程序中有一个名为“Photo”的RESTful资源.我正在使用 Paperclip来为我的照片提供不同的“样式”(缩略图等),我正在使用自定义路径来RESTful访问这些样式:
map.connect "photos/:id/style/*style",:controller => "photos",:action => "show"

这工作正常,但我想写一个测试,以确保它保持这种方式.

我已经有一个功能测试来调用Photo控制器的show动作(实际上是由scaffold生成):

test "should show photo" do
  get :show,:id => photos(:one).to_param
  assert_response :success
end

它测试URL“/ photo / 1”处的动作执行情况.现在我想测试URL“/ photo / 1 / style / foo”的执行情况.不幸的是,我似乎无法让ActionController :: TestCase点击该URL; get方法总是需要一个action / id,并且不接受URL后缀.

如何测试自定义网址?

更新

在检查@ fernyb的答案时,我在the same rdoc找到了这个片段

In tests you can simply pass the URL or named route to get or post.
def send_to_jail
get ‘/jail’
assert_response :success
assert_template “jail/front”
end

但是,当我实际尝试时,我收到一条错误消息:

test "should get photo" do
  get "/photos/1/style/original"
  assert_equal( "image/jpeg",@response.content_type )
end  

ActionController::RoutingError: No route matches {:action=>"/photos/1/style/original",:controller=>"photos"}

我想知道我做错了什么.

解决方法

使用assert_routing测试路由:
assert_routing("/photos/10/style",:action => "show",:id => "10",:style => [])

assert_routing("/photos/10/style/cool",:style => ["cool"])

assert_routing("/photos/10/style/cool/and/awesome",:style => ["cool","and","awesome"])

在集成测试中,您可以执行以下操作:

test "get photos" do
   get "/photos/10/style/cool"
   assert_response :success
end

原文地址:https://www.jb51.cc/ruby/269738.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐