提问人:Andrew Grimm 提问时间:11/4/2015 最后编辑:CommunityAndrew Grimm 更新时间:11/4/2015 访问量:220
为什么 eql 在 RSpec 规范中比 eql 更常见?在普通代码中?
Why is eql more common in RSpec specs than eql? in normal code?
问:
在许多使用 RSpec 的 Ruby 或 Ruby on Rails 项目中,在规范文件中使用得相当频繁,但在规范文件之外很少使用,而在规范文件中使用频率不高,但经常在规范文件之外使用。eql
eql?
==
例如,查看 spree 项目:
spree agrimm$ git grep eql **/spec/**/* | wc
135 589 14976
spree agrimm$ git grep eql | wc
138 600 15274
spree agrimm$ git grep '==' **/spec/**/* | wc
56 413 5764
spree agrimm$ git grep '==' | wc
1165 11857 297297
我了解 和 之间的功能差异。但是,为什么人们经常选择在RSpec中使用(这是对应于)的匹配器(例如)而不是(例如)呢?==
eql?
eql
eql?
expect(a).to eql(b)
==
expect(a).to be == b
答:
0赞
Andrew Grimm
11/4/2015
#1
这只是一个猜测,而不是确切的知识。其他人可能肯定知道。
# Tests whether a == b
expect(a).to be == b
看起来不如
# Tests whether a.eql?(b)
expect(a).to eql(b)
但有些用户不知道你可以用它来给予eq
# Tests whether a == b
expect(a).to eq(b)
Spree 项目相当频繁地使用:eq
spree agrimm$ git grep eq **/spec/**/* | wc
2930 11694 320369
评论
0赞
Sergio Tulentsev
11/4/2015
例如,我从不使用。只。eql
eq
-1赞
Peter Alfvin
11/4/2015
#2
==
在当前 RSpec 文件中很少出现,因为首选语法不支持匹配器。它经常出现在实现文件中,因为它是更自然的语法。==
expect
类似地,(和)在 RSpec 中经常出现,因为它们分别是 和 的匹配器。在生产中几乎不需要它们,因为占主导地位。eql
eq
eql?
==
==
评论
0
0.0
0
0.0