DHH 关于单元测试:RSpec 确实不必要地复杂吗?

发布于 2024-10-27 15:12:39 字数 320 浏览 1 评论 0原文

我碰巧是 Ruby Inside 的订阅者,因为我对 Rails 特别感兴趣。昨天,Rails 的创建者 David Heinemeier Hansson 几乎说他只是使用测试/单元。我会理解,因为这是 Rails 内部的,但他似乎给出了强烈的意见。他认为 RSpec 和 Cucumber 过于复杂。

我通常不会太在意,但这取决于谁说了什么。我非常尊重汉森,他的观点让我思考。当我开始使用 Rails 时,我从未真正研究过测试/单元。只有 RSpec 和 Cucumber。

这就是为什么我需要你的见解。你是否认为RSpec确实很复杂,没有太多附加值?编写测试/单元是否需要更少的时间和精力?

I happen to be a subscriber of Ruby Inside, since I'm particularly interested in Rails. Yesterday, the creator of Rails, David Heinemeier Hansson, pretty much said that he's just using test/unit. I would understand that, since it's Rails internal, but he seems to have given a strong opinion. He believes that RSpec and Cucumber are needlessly complicated.

I would normally not pay much attention, but it depends on who says something. I respect Hansson a lot and his opinion got me thinking. When I started with Rails, I never really looked into test/unit. Just RSpec and Cucumber.

And that is why I want your insight. Do you think that RSpec is indeed complicated for not much added value? Does writing test/unit take less time and effort?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱的那么颓废 2024-11-03 15:12:39

我的建议是使用 Shoulda(扩展 Test::Unit)或 RSpec 与 Capybara,而不是 Cucumber。

我认为对嵌套上下文使用 RSpec 或 Shoulda 绝对值得这样做。不过,RSpec 绝对是重量级的(也许是超重的),因此我对它持观望态度。

我终于明白了,黄瓜比它通常的价值要麻烦得多。您可以通过简单的集成测试和 Capybara 更简单、更稳健地完成您需要的任务。请记住——水豚!=黄瓜,而水豚本身就非常有能力。

Shoulda 很好,因为它只是为标准 Test::Unit 框架添加了便利,因此比 RSpec 轻量得多(从技术上讲,每个框架都解决一组不同的问题,但它们都提供嵌套上下文功能)。 RSpec 的优点是使断言读取起来更自然,并且在许多情况下还可以生成更有用的失败消息,而无需在断言上编写消息参数。

另外,请记住,Cucumber 实际上并不需要 RSpec,因此如果您想继续使用 Cucumber,只需使用 Test::Unit 即可。选择比比皆是。

My recommendation would be to use either Shoulda (extends Test::Unit) or RSpec with Capybara, and -no- Cucumber.

I think that the use of either RSpec or Shoulda for nested contexts is definitely worth doing. RSpec is definitely heavy-weight (perhaps overweight) though, and I'm on the fence with it for that reason.

Cucumber, I've finally come to understand, is waaay more cumbersome than it's usually worth. You can accomplish what you need more simply and robustly with plain ol' integration tests and Capybara. Remember -- Capybara != Cucumber, and Capybara is quite capable all on its own.

Shoulda is nice, because it simply adds conveniences to the standard Test::Unit framework, and is therefore much lighter-weight than RSpec (technically, each solves a different set of problems, but they both provide nested-context capabilities). RSpec has the advantage of making assertions read more naturally, and also generating more helpful failure messages in many cases, without the need for writing message arguments on the assertions.

Also, remember that Cucumber does not actually require RSpec, so if you want to keep using Cucumber, you can do that with just Test::Unit. Choices abound.

镜花水月 2024-11-03 15:12:39

这都是关于语义的。 RSpec 和 Test::Unit 在功能上类似。就我个人而言,我一直更喜欢 RSpec,因为我发现使用它编写测试更自然。我还喜欢编写自定义匹配器的简单性,并且提供的默认匹配器很有用。

黄瓜是一种完全不同的野兽。是的,如果您没有正确组织步骤定义,它相当麻烦并且变得难以维护,但它确实有一个非常强大的用例,那就是当您的客户编写规范时。

我曾参与过客户与我们的 QA 团队之一一起编写 Cucumber 场景的项目。作为非技术人员,在代码中指定用户故事是一种极其平易近人且自然的方式。在遵循敏捷实践方面,Cucumber 确实帮助我们做到了这一点。最终产品的质量受益于此,但我不喜欢 Cucumber 作为开发人员:)

It's all about semantics. RSpec and Test::Unit are similar functionally. Personally I've always preferred RSpec because I found it more natural to write tests using it. I also like the simplicity of writing custom matchers, and the default matchers provided are useful.

Cucumber is a different beast entirely. Yes it's fairly cumbersome and becomes hard to maintain if you don't organise your step definitions properly but it does have one very strong use case and that is when your client is writing your specifications.

I've worked on projects where the client has been writing Cucumber scenarios together with one of our QA team. As a non-technical person it's an extremely approachable and natural way to specify user stories in the code. Cucumber really helped us walk the walk when it came to following our agile practices. The quality of the end product benefitted from that but no I do not like Cucumber as a developer :)

薔薇婲 2024-11-03 15:12:39

这是个人品味的问题。

我喜欢编写简单的黄瓜测试而不用担心细节。只是测试我的应用程序的“快乐”路径。 (便携、易于理解、缓慢)

我将细节留给测试/单元。 (简单,快速)

需要更多时间来理解:

get :products, :session => @session_id_for_product_banana
assert_select "table" do
  assert_select "td#name", "Banana"
end

而不是

When I go to the banana page
Then I should see "Banana"

确定这些测试不相等,但谁在乎“Banana”是否在 div 或表格中,或者没有正确的 html-id。

我不喜欢功能测试,因为重构后,id 可能会消失,会话期望可能会改变。如果是这种情况,您将需要重构代码和测试。如果您使用黄瓜,则不必更改您的场景。

It's a matter of personal taste.

I like to write easy cucumber tests without worrying about details. Just testing the "happy" paths of my app. (portable, understandable, slow)

I leave the details to Test/Unit. (easy, fast)

It takes more time to understand:

get :products, :session => @session_id_for_product_banana
assert_select "table" do
  assert_select "td#name", "Banana"
end

instead of

When I go to the banana page
Then I should see "Banana"

Sure those tests are not equal but who cares whether "Banana" is in a div or a table or doesn't have the right html-id.

I dont' like functional tests because after refactoring, the id's could be gone, the session expectation could be changed. If that's the case you will need to refactor your code AND tests. If you'd use cucumber you wouldn't have to change your scenario's.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文