ruby – Rspec`eq` vs`eql`在`expect`测试中
发布时间:2020-12-16 22:30:57 所属栏目:百科 来源:网络整理
导读:在rspec测试中使用eq和eql有什么区别?是否有区别: 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.ent
在rspec测试中使用eq和eql有什么区别?是否有区别:
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','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忽略类型转换. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |