问题描述
在 RSpec 中,使用 should == ...
和 should eql(...)
有什么区别?我注意到 RSpec 文档总是使用 eql
,但 ==
输入更少且更易于阅读.我错过了什么?
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?
推荐答案
其实很简单:should ==
将 ==
消息发送给测试对象,should eql
向测试对象发送eql?
消息.换句话说:两个不同的测试发送两个完全不同的消息,它们调用两个完全不同的方法,从而做两个完全不同的事情.特别是,eql?
比 ==
更严格,但不如 equals?
严格.
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?
.
这篇关于RSpec:“should == ..."之间的区别;和“应该 eql(...)";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!