任意或自定义URL的Railsfunction测试

我的Rails应用程序中有一个名为“Photo”的RESTful资源。 我正在使用Paperclip来为我的照片提供不同的“样式”(缩略图等),并且我使用自定义路径来RESTful访问这些样式:

map.connect "photos/:id/style/*style", :controller => "photos", :action => "show" 

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

我已经有一个function测试来调用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后缀。

如何测试自定义url?

更新

在检查@ fernyb的答案时,我在同一个rdoc中找到了这个片段

在测试中,您只需传递URL或命名路由即可获取或发布。 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", :controller => "photos", :action => "show", :id => "10", :style => []) assert_routing("/photos/10/style/cool", :controller => "photos", :action => "show", :id => "10", :style => ["cool"]) assert_routing("/photos/10/style/cool/and/awesome", :controller => "photos", :action => "show", :id => "10", :style => ["cool", "and", "awesome"]) 

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

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

从Rails API文档:

路线通配

*[string]指定为规则的一部分,如:

 map.connect '*path' , :controller => 'blog' , :action => 'unrecognized?' 

将覆盖之前未识别的路线的所有剩余部分。 globbed值在params[:path]作为路径段的数组。

所以看起来你需要传递:path参数,以正确地测试动作。