在rspec测试中使用eqeql有什么区别?这两者之间有区别吗

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

以及:

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

推荐答案

根据比较中使用的等式类型,这里存在细微的差异.

从Rpsec文档:

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忽略类型转换.

Ruby相关问答推荐

如何使用Ruby range?

我可以在 Ruby 中使用默认值创建一个数组吗?

为什么在Ruby中用空格分隔的两个字符串连接在一起?

删除 Ruby 数组中的 nil 和空白字符串

如何在 linux (ubuntu) 上更新 ruby​​?

在文件中搜索字符串的最佳方法是什么?

来自 Linux 上的命令队列的并行处理(bash、python、ruby ......随便)

在 Ruby 中重写 to_s 方法不好吗?

如何获取符号链接的目标?

Ruby 的所有/最佳列表?

Ruby RVM apt-get 更新错误

如何在 Ruby 中获取命名空间中的所有类名?

Ruby 中的=~运算符是什么?

如何在 Ruby 中将类构造函数设为私有?

从Ruby中的子类方法调用父类中的方法

Ruby Style:如何判断嵌套的哈希元素是否存在

通过 factory_girl 协会查找或创建记录

等号 ('=') 放在方法定义中的方法名称之后有什么作用?

为什么 Ruby 有 TrueClass 和 FalseClass 而不是一个布尔类?

def `self.function` 名称是什么意思?