本文介绍了`expect` 测试中的 Rspec `eq` 与 `eql`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 rspec 测试中使用 eqeql 有什么区别?是否有区别:

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', '[email protected]')
  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('[email protected]')
end

和:

it "adds the correct information to entries" do
  # book = AddressBook.new # => Replaced by line 4
  book.add_entry('Ada Lovelace', '010.012.1815', '[email protected]')
  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('[email protected]')
end

推荐答案

根据比较中使用的相等类型,此处存在细微差别.

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

来自 Rpsec 文档:

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 使用 == 运算符进行比较,eql 忽略类型转换.

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

这篇关于`expect` 测试中的 Rspec `eq` 与 `eql`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 07:59