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

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

问题描述

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

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

现在,我现在写的是:


  • 测试) - 这些测试针对我们的应用程序返回的HTML / JS,但有时也测试其他内容,例如调用第三方服务。

  • RSpec控制器测试(功能测试)

  • RSpec模型测试(单元测试)

有时这是完全必要的;有必要测试模型中对终端用户不完全明显或不可见的行为。当模型很复杂时,它们应该被测试。但其他时候,在我看来,测试是多余的。例如,如果只是通过 bar bar调用,则测试方法 foo 已测试?如果 bar 是一个简单的帮助程序方法,在模型上使用,并且可以在Cucumber功能中容易测试?你测试的方法在rspec以及黄瓜?我发现自己在努力,因为写更多的测试需要时间和维护多个版本的什么是有效的相同的行为,这使得维护测试套件更加时间密集,这反过来使变更更昂贵。

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.

简而言之,你相信有时候只写黄瓜功能就够了吗?或者你应该总是在每个级别测试?如果你认为有一个灰色区域,你的阈值为这需要功能/单元测试。实际上,你现在做什么,为什么(或为什么不)你认为足够了?

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?

编辑这里有一个例子,可能是测试overkill。诚然,

推荐答案

很好的问题,我最近刚刚抓住了而在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.

使用Outside-in测试,我的过程通常像: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.

我经常采取的一个步骤是在我的黄瓜功能运行rcov rake cucumber: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/

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

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