有没有办法提供更好的 rspec 测试描述

发布于 2025-01-07 23:01:17 字数 378 浏览 1 评论 0原文

我已经使用 rspec 一段时间了,最​​近将样式从

it "should do something cool" do
  @something.should work
end

更简洁的样式切换为更简洁的

subject(@something)

it {should work}

样式,虽然在编写测试和在代码中查看它们时我更喜欢更简洁的样式,但我怀念能够为每个测试指定描述的功能,特别是因为相等消息仅显示正在测试的值。因此,在上面的示例中,假设测试通过,使用第一种样式,我会收到一条消息说“它应该做一些很酷的事情”,而第二种样式只会说它已经起作用。

有谁知道有什么方法可以做到这一点?干杯

I have been using rspec for a little while and recently switched style from

it "should do something cool" do
  @something.should work
end

to the more concise

subject(@something)

it {should work}

Whilst I much prefer the more concise style when writing the tests and for viewing them in code, I miss being able to specify the description for each test, particularly since the equality messages just display the values that are being tested. So in the example above, assuming the test passes, with the first style, I would get a message saying 'it should do something cool', whereas the second will just say that it has worked.

Does anyone know of a way to do this? Cheers

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

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

发布评论

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

评论(1

尹雨沫 2025-01-14 23:01:17

我认为您对 specify/subject 块的作用有错误的概念。它们并不是要完全替代您所切换的更详细的语法,而是应该在不需要描述时使用。

因此,如果您想要描述,只需使用

it "should do something cool" do
  @something.should work
end

另外,我个人认为 specify/subject 并不更简洁。对我来说,这距离使用 rspec 读取创建的更像 DSL 的规范方式还有一步之遥,但这可能是个人喜好的问题。

I think you got the wrong concept of what the specify/subject blocks do. They are not meant to be a complete replacement for the more verbose syntax you switched from, but should be used when there is no need for a desciption.

So if you want a desciption, just use

it "should do something cool" do
  @something.should work
end

Also, I personally don't think that the specify/subject is more concise. For me it's a step away from the more DSL-like way specifications created with rspec read, but that might be a matter of personal preference.

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