gpt4 book ai didi

ruby - Rspec `eq` 与 `eql` 测试中的 `expect`

转载 作者:数据小太阳 更新时间:2023-10-29 06:27:42 27 4
gpt4 key购买 nike

在 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 - Rspec `eq` 与 `eql` 测试中的 `expect`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32926817/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com