我的Rails应用程序中有一个RESTful资源,称为“照片”。我正在使用Paperclip提供我照片的不同“样式”(用于缩略图等),并且正在使用自定义路由来RESTful访问这些样式:
map.connect "photos/:id/style/*style", :controller => "photos", :action => "show"
很好,但是我想编写一个测试以确保它保持这种状态。
我已经有一个功能测试来调用Photo Controller 的show Action (实际上是由脚手架生成的):
test "should show photo" do
get :show, :id => photos(:one).to_param
assert_response :success
end
这将在URL“/photo/1”上测试 Action 的执行情况。现在,我要测试URL“/photo/1/style/foo”的执行情况。不幸的是,我似乎无法通过ActionController::TestCase来访问该URL。 get方法始终需要一个操作/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", :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
关于ruby-on-rails - 任意或自定义URL的Rails功能测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1656285/