在 RSpec 中测试期望时,首选哪种风格,lambda..should 或expect..to?
我看到这两种样式被广泛使用:#1 lambda { raise "Boom" }.should raise_error
和 #2 expect { raise "Boom" }.to raise_error
。我喜欢expect..to more,因为它读起来更好并且隐藏了过程的创建。
我查看了 rspec 代码,似乎期望..to是建议< /a>,但是我经常遇到使用 lambda..should 的库。是期望..更新,因此还不是“著名的”吗?
I have seen both styles used widely: #1 lambda { raise "Boom" }.should raise_error
and #2 expect { raise "Boom" }.to raise_error
. I like expect..to more as it reads better and hides the creation of the proc.
I looked at rspec code and it seems expect..to is suggested, however I regularly come across libraries using lambda..should. Is expect..to newer and hence not "famous" yet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
expect
自 rspec-2 起使用,之前必须使用lambda
。RSpec “官方”建议使用
expect
并且他们可能会决定“废弃”lambda 语法。大多数在 RSpec1 中诞生的库都使用了 lambda 语法。他们只是还没有迁移(如果仍然支持的话他们为什么要迁移)。
因此,请使用
expect
而不是lambda
。expect
is used since rspec-2, previouslylambda
had to be used.RSpec "officially" recommends to use
expect
and it is possible that they will decide to "obsolete" lambda syntax.The lambda syntax is used in most of the libraries that started life in RSpec1 days. They just haven't yet migrated (and why would they if it is still supported).
So, do use
expect
instead oflambda
.