加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 04:40:11 所属栏目:百科 来源:网络整理
导读:我的Rails应用程序中有一个名为“Photo”的RESTful资源.我正在使用 Paperclip来为我的照片提供不同的“样式”(缩略图等),我正在使用自定义路径来RESTful访问这些样式: map.connect "photos/:id/style/*style",:controller = "photos",:action = "show" 这工
我的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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读