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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...