Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec: difference between "should == ..." and "should 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?

like image 427
nicholaides Avatar asked Jul 19 '10 21:07

nicholaides


2 Answers

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?.

like image 122
Jörg W Mittag Avatar answered Nov 15 '22 11:11

Jörg W Mittag


They are usually equivalent, but not always:

1 ==   1.0 # => true
1.eql? 1.0 # => false
like image 42
Marc-André Lafortune Avatar answered Nov 15 '22 10:11

Marc-André Lafortune