RSpec: difference between “should == …” and “should eql(…)”

北城余情 提交于 2020-01-23 05:16:15

问题


In RSpec, what's the difference between using should == ... and should eql(...)? I noticed that the RSpec documentation always uses eql, but == is less typing and easier to read. What am I missing?


回答1:


It's rather simple, really: should == sends the == message to the test subject, should eql sends the eql? message to the test subject. In other words: the two different tests send two completely different messages which invoke two completely different methods and thus do two completely different things. In particular, eql? is stricter than == but less strict than equals?.




回答2:


They are usually equivalent, but not always:

1 ==   1.0 # => true
1.eql? 1.0 # => false


来源:https://stackoverflow.com/questions/3285199/rspec-difference-between-should-and-should-eql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!