反对在 RSpec 测试中使用 before、let 和 subject 的论据是什么? [英] What is the argument against using before, let and subject in RSpec tests?

查看:27
本文介绍了反对在 RSpec 测试中使用 before、let 和 subject 的论据是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始编写 RSpec 测试,我遇到了 thoughtbot 的样式指南,建议不要使用 letlet!beforesubject(以及其他).

I just started writing RSpec tests, and I came across thoughtbot's Style Guide, which recommends against let, let!, before and subject (among others).

我还在其他几个地方读到过类似的建议(包括旧的 RSpec docs 警告关于 before(:all)),但我似乎无法找到反对它们的实际论据.

I've also read similar suggestions in a few other places (including the old RSpec docs warning about before(:all)), but I can't seem to find the actual argument against them.

所以问题是:

为什么我不应该在测试中使用这些方法?什么是更好的方法?

推荐答案

有趣的问题;一些我也想了解更多的东西......所以挖了一点,这是我发现的:

Interesting question; something that I want to know more about as well.... So dug in a bit, and here is what I uncovered:

  1. 早期版本的样式指南中,该声明还有更多内容:

  1. In an earlier version of the style guide, there's more to that statement:

避免它、让、让!、指定、主题和其他 DSL.更喜欢明确性和一致性.

ThoughtBot 的人发表了一篇关于 let name 我们不要的帖子.另请参阅链接至 Github 提交评论对话

ThoughtBot folks made a post on let name let's not. See also the link to the Github Commit Comment conversation

ThoughtBot 的首席技术官在他们最近的一个播客 Joe Ferris 中解释了为什么它不是一个使用 letsubject 的好主意.查看 27 分钟 37 秒的标题为 Something Else Was Smellier 的播客接下来的 5 分钟.

On one of their recent podcasts Joe Ferris, the CTO of ThoughtBot, explains why it's not a good idea to use let and subject. Check out the podcast titled Something Else Was Smellier from the 27m37s mark onwards for the next 5 minutes.

测试反模式神秘访客",在中有详细介绍一篇较旧的 ThoughtBot 博文 是为什么不使用 let 及其表亲的主要原因.

Testing Anti-pattern 'Mystery Guest' which is dealt with in detail in an older ThoughtBot blogpost is the main reason for why not to use let and its cousins.

非常简洁地总结一下我对以上所有内容的理解:

To summarize my understanding of all the above very succinctly:

使用 let 等会让人难以理解发生了什么在测试中一目了然,并要求该人花费一些时间来建立联系.

Using let et al makes it difficult to understand what's happening within the test on a quick glance, and requires the person to spend some time in making the connections.

编写易于理解的测试,无需太多努力.

Write tests such that it is easy to understand without much effort.

此外,在测试中大量使用 let 会导致测试之间的过度共享,以及隐含的 common fixtures - 即有一个共同的 fixture 开始对于正在编写的每个测试,即使它不适用.

In addition, using let liberally in tests results in over-sharing among the tests, as well as makes implicit common fixtures - i.e. having a common fixture to start with for every test being written even when it does not apply.

before(:all)

反对使用 before(:all) 的论点是直截了当的.如旧 rspec 文档 中所述:

before(:all)

The argument against using before(:all) is straight-forward. As explained in the old rspec documentation:

警告:通常不鼓励使用 before(:all) 和 after(:all),因为它会引入示例之间的依赖关系.不过,如果您知道自己在做什么,它可能对非常昂贵的操作很有用.

Warning: The use of before(:all) and after(:all) is generally discouraged because it introduces dependencies between the Examples. Still, it might prove useful for very expensive operations if you know what you are doing.

before(:all) 在 ExampleGroup 开始时只执行一次.因此,可能会在不经意间引入示例之间的依赖关系.Thoughtbot 关于测试不易理解的断言也适用于此.

before(:all) gets executed only once at the start of the ExampleGroup. As such there is a potential to inadvertently introduce dependencies between the Examples. Thoughtbot's assertion about the tests not being easy to understand applies to this as well.

总而言之,编写更好的规范的建议似乎是:

In conclusion, the advise for writing better specs seems to be:

  1. 编写测试,以便快速浏览时易于理解.
  2. 知道你在做什么.

这篇关于反对在 RSpec 测试中使用 before、let 和 subject 的论据是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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