本文介绍了Rails对任意或自定义URL的功能测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在我的Rails应用程序中有一个RESTful资源称为照片。我使用回形针来为我的照片提供不同的样式(缩略图等) ),我使用自定义路线来REST式访问这些样式: map.connectphotos /:id / style / * style,:controller => photos,:action => show 这很好,但我想写一个测试, 。 我已经有一个功能测试来调用Photo控制器的show action(事实上由scaffold生成): test应该显示照片do get:show,:id =>照片(:一).to_param assert_response:success end 在URL/ photo / 1执行动作。现在我想测试URL/ photo / 1 / style / foo的执行。不幸的是,我似乎不能得到ActionController :: TestCase命中该URL; get方法总是想要一个动作/ id,不接受URL后缀。 如何测试自定义URL? 更新 在检查@ fernyb的回答时,我发现这个片段在同一个rdoc 但是,当我实际尝试时,会收到一条错误消息: testshould get photodo get/ photos / 1 / style / original assert_equal(image / jpeg,@ response.content_type) end ActionController :: RoutingError:没有路由匹配{:action =>/ photos / 1 / style / original,:controller =>photos} 解决方案 div> 使用 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 => ]) assert_routing(/ photos / 10 / style / cool / and / awesome,:controller => photos,:action => show,:id => 10,:style => [cool,and,awesome]) ,您可以执行以下操作: 测试获取照片do get/ photos / style / cool assert_response:success end I have a RESTful resource in my Rails app called "Photo". I'm using Paperclip to serve different "styles" of my photos (for thumbnails and the like), and I'm using a custom route to RESTfully access those styles:map.connect "photos/:id/style/*style", :controller => "photos", :action => "show"That's working fine, but I want to write a test to make sure it stays that way. I already have a functional test to call the Photo controller's show action (generated by scaffold in fact):test "should show photo" do get :show, :id => photos(:one).to_param assert_response :successendThat tests the execution of the action at the URL "/photo/1". Now I want to test the execution of the URL "/photo/1/style/foo". Unfortunately, I can't seem to get ActionController::TestCase to hit that URL; the get method always wants an action/id and won't accept a URL suffix.How do I go about testing a custom URL?UpdateWhile checking on @fernyb's answer I found this snippet in the same rdocHowever, when I actually try that, I get an error message: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"}I wonder if I'm doing something wrong. 解决方案 Use assert_routing to test routes: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"])In your integration test you can then do:test "get photos" do get "/photos/10/style/cool" assert_response :successend 这篇关于Rails对任意或自定义URL的功能测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-21 11:08