在 Rails 路由中测试什么? [英] What to test in Rails routing?

查看:51
本文介绍了在 Rails 路由中测试什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇人们认为什么是充分/彻底的路线测试.一个和我一起工作的人似乎想在我们的路由文件中声明每条路由,无论多么标准.我觉得这是在浪费时间,但也许我错了,我不知道这有一些价值.

I'm curious what people consider adequate/thorough testing of routes. A guy I work with seems to want to assert every route in our routes file, no matter how standard. I feel like this is a waste of time, but maybe I'm wrong and there is some value to this I'm unaware of.

在某些情况下,我可以在路由中看到一些价值.我们仍然有一些动作可以同时响应 GET 和 POST 请求,尽管我一直想摆脱它们.我们对 lambdas 或任何东西没有任何疯狂的限制,但如果我们这样做,似乎值得测试.

There are a few cases where I can see some value in routing. We still have a couple actions that respond to both GET and POST requests, although I've been meaning to get rid of those. We don't have any kind of crazy constraints with lambdas or anything, but that seems like it would be worth testing if we did.

但是对于普通的资源定义?

But for a normal resources definition?

resources :foo, only: [:index, :show]

我们断言这两个路由都存在,我们断言它们是 GET 并且它们转到正确的控制器/操作.这有什么道理吗?感觉现在我们只是在测试 Rails.

We have assertions that both of these routes exist, we assert that they're GET and that they go to the correct controller/action. Is there any point to any of that? It feels like we're just testing Rails at this point.

在一个稍微相关的问题上,我更喜欢像上面那样定义资源路由(使用 only: [:index, :show] 部分).如果该控制器上只有索引/显示操作,那么只在路由文件中定义 resources :foo 有什么后果吗?

On a slightly related question, I prefer to have resource routes defined like the one above (with the only: [:index, :show] part). Are there any consequence to only defining resources :foo in the routes file if there are only index/show actions on that controller?

在我看来,它可能只是使用了更多的时间和/或内存,但它是否也是出于某种安全问题或我不知道的非常糟糕的事情?

It seems to me that it's probably just using more time and/or memory, but is it somehow also a security concern or something really bad that I'm unaware of?

推荐答案

测试路由的最大原因不是重复测试 Rails,而是验证面向公众的 API.

The biggest reasons to test routes isn't to double-test Rails, but rather to verify a public-facing API.

这减少了针对广告入口点的回归,而不管它们获取或返回的数据如何.

This reduces regressions against advertised entry points irrespective of the data they take or return.

在什么级别测试这些也有一些争论;测试路线本身是否有价值,还是只有验证进/出的数据才有意义?这样做测试相同的路线,我认为更有价值——但速度更慢.

What level to test these at is open to some debate as well; is it valuable to test the route itself, or does it only make sense to also verify data going in/coming out? Doing so also tests the same routes, and I'd argue is more valuable–but slower.

我倾向于只在以下情况下直接测试路由:

I tend to only test routes directly when:

  • 它们有点时髦,例如,不是沼泽标准路线
  • 必须拒绝一些路由
  • 在初步开发期间.

初步路线测试通常会在以其他方式进行测试后被删除.

Preliminary route tests generally get deleted once they're tested in other ways.

这篇关于在 Rails 路由中测试什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆