2015-10-03 14 views
17

rspecテストでeqeqlを使用する違いは何ですか?rspec `eq`と` expect`テストのeql`

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 

答えて

23

微妙な違いが比較に使用されている平等のタイプに基づいて、ここにありとの間に違いはあります。 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は、型変換を無視します。

+0

タイプ変換は、同じタイプのオブジェクトであると比較されるオブジェクトまたは物を探していることを意味しますか? – austinthesing

+5

これは、 '42.0 == 42'が' true'と '42.0.eql 'を生成することを意味していますか? 42は 'false'を生成する。 –

関連する問題