带有 Cucumber 和 rspec 的 BDD - 这什么时候是多余的? [英] BDD with Cucumber and rspec - when is this redundant?

查看:20
本文介绍了带有 Cucumber 和 rspec 的 BDD - 这什么时候是多余的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails/工具特定版本:您的单元测试有多深?

A Rails/tool specific version of: How deep are your unit tests?

现在,我正在写:

  • Cucumber 功能(集成测试)- 这些测试针对我们的应用返回的 HTML/JS,但有时也会测试其他内容,例如对第三方服务的调用.
  • RSpec 控制器测试(功能测试),最初仅当控制器具有任何有意义的逻辑时才进行,但现在越来越多.
  • RSpec 模型测试(单元测试)

有时这是完全必要的;有必要测试模型中对最终用户不完全明显或不可见的行为.当模型很复杂时,肯定应该对它们进行测试.但其他时候,在我看来这些测试是多余的.例如,如果 foo 方法只被 bar 调用,并且 bar 被测试,你会测试它吗?如果 bar 是模型上的一个简单的辅助方法,可以在 Cucumber 特征中使用并且易于测试,那会怎样?您是否在 rspec 和 Cucumber 中测试该方法?我发现自己在为此苦苦挣扎,因为编写更多测试需要时间并维护有效相同行为的多个版本",这使得维护测试套件的时间更加密集,从而使更改成本更高.

Sometimes this is entirely necessary; it is necessary to test behavior in the model that is not entirely obvious or visible to the end-user. When models are complex, they should definitely be tested. But other times, it seems to me the tests are redundant. For instance, do you test method foo if it is only called by bar, and bar is tested? What if bar is a simple helper method on a model that is used by and easily testable in a Cucumber feature? Do you test the method in rspec as well as Cucumber? I find myself struggling with this, as writing more tests take time and maintaining multiple "versions" of what is effectively the same behaviors, which makes maintaining the test suite more time intensive, which in turn makes changes more expensive.

简而言之,您是否相信有一段时间只编写 Cucumber 功能就足够了?还是应该始终在每个级别进行测试?如果您认为存在灰色区域,那么这需要功能/单元测试"的阈值是多少.实际上,您目前在做什么,为什么(或为什么不)您认为这已经足够了?

In short, do you believe there is there a time when writing only Cucumber features is enough? Or should you always test at every level? If you think there is a grey area, what is your threshold for "this needs a functional/unit test." In practical terms, what do you do currently, and why (or why not) do you think it's sufficient?

编辑:以下是测试过度杀伤"的示例. 诚然,我能够很快写出这篇文章,但这完全是假设性的.

EDIT: Here's an example of what might be "test overkill." Admittedly, I was able to write this pretty quickly, but it was completely hypothetical.

推荐答案

好问题,我最近在开发 Rails 应用程序时遇到了一个问题,也使用 Cucumber/RSpec.我尝试在每个级别尽可能多地进行测试,但是,我也发现随着代码库的增长,我有时会觉得我在不必要地重复自己.

Good question, one I've grappled with recently while working on a Rails app, also using Cucumber/RSpec. I try to test as much as possible at every level, however, I've also found that as the codebase grows, I sometimes feel I'm repeating myself needlessly.

使用由外到内"测试,我的过程通常是这样的:Cucumber Scenario -> Controller Spec -> Model Spec.我越来越发现自己跳过了控制器规格,因为黄瓜场景涵盖了它们的大部分功能.我通常会回去添加控制器规格,但这感觉有点麻烦.

Using "Outside-in" testing, my process usually goes something like: Cucumber Scenario -> Controller Spec -> Model Spec. More and more I find myself skipping over the controller specs as the cucumber scenarios cover much of their functionality. I usually go back and add the controller specs, but it can feel like a bit of a chore.

我经常采取的一个步骤是使用 rake Caribbean:rcov 在我的黄瓜特征上运行 rcov 并寻找覆盖范围内的显着差距.这些是我确保重点关注的代码区域,因此它们具有良好的覆盖率,无论是单元测试还是集成测试.

One step I take regularly is to run rcov on my cucumber features with rake cucumber:rcov and look for notable gaps in coverage. These are areas of the code I make sure to focus on so they have decent coverage, be it unit or integration tests.

我相信模型/库应该立即进行广泛的单元测试,因为它是核心业务逻辑.它需要在正常的 Web 请求/响应过程之外独立工作.例如,如果我通过 Rails 控制台与我的应用程序交互,那么我将直接处理业务逻辑,并且我希望确保我在模型/类上调用的方法经过良好测试.

I believe models/libs should be unit tested extensively, right off the bat, as it is the core business logic. It needs to work in isolation, outside of the normal web request/response process. For example, if I'm interacting with my app through the Rails console, I'm working directly with the business logic and I want the reassurance that methods I call on my models/classes are well tested.

归根结底,每个应用程序都是不同的,我认为这取决于开发人员来确定应该将多少测试覆盖范围用于代码库的不同部分并找到正确的平衡,以便您的测试套件随着应用的发展,不会让您陷入困境.

At the end of the day, every app is different and I think it's down to the developer(s) to determine how much test coverage should be devoted to different parts of the codebase and find the right balance so that your test suite doesn't bog you down as your app grows.

这是我从书签中挖出的一篇有趣的文章,值得一读:http://webmozarts.com/2010/03/15/why-not-to-want-100-code-coverage/

Here's an interesting article I dug up from my bookmarks that is worth reading: http://webmozarts.com/2010/03/15/why-not-to-want-100-code-coverage/

这篇关于带有 Cucumber 和 rspec 的 BDD - 这什么时候是多余的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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