Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What is the difference between using eq and eql in rspec tests? Is there a difference between:

it "adds the correct information to entries" do
  # book = AddressBook.new # => Replaced by line 4
  book.add_entry('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
  new_entry = book.entries[0]

  expect(new_entry.name).to eq('Ada Lovelace')
  expect(new_entry.phone_number).to eq('010.012.1815')
  expect(new_entry.email).to eq('augusta.king@lovelace.com')
end

and:

it "adds the correct information to entries" do
  # book = AddressBook.new # => Replaced by line 4
  book.add_entry('Ada Lovelace', '010.012.1815', 'augusta.king@lovelace.com')
  new_entry = book.entries[0]

  expect(new_entry.name).to eql('Ada Lovelace')
  expect(new_entry.phone_number).to eql('010.012.1815')
  expect(new_entry.email).to eql('augusta.king@lovelace.com')
end
question from:https://stackoverflow.com/questions/32926817/rspec-eq-vs-eql-in-expect-tests

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
375 views
Welcome To Ask or Share your Answers For Others

1 Answer

There are subtle differences here, based on the type of equality being used in the comparison.

From the Rpsec docs:

Ruby exposes several different methods for handling equality:

a.equal?(b) # object identity - a and b refer to the same object
a.eql?(b) # object equivalence - a and b have the same value
a == b # object equivalence - a and b have the same value with type conversions]

eq uses the == operator for comparison, and eql ignores type conversions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...