2015-10-03 11 views
17

Was ist der Unterschied zwischen der Verwendung von eq und eql in RSPC-Tests? Gibt es einen Unterschied zwischen:Rspec `eq` vs` eql` in `expect` Tests

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 

und:

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 

Antwort

23

Es gibt subtile Unterschiede hier, basierend auf der Art der Gleichheit im Vergleich verwendet wird.

Aus dem 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 verwendet den == Operator für den Vergleich und eql ignoriert Typkonvertierungen.

+0

Würde eine Typkonvertierung bedeuten, dass die Objekte oder Dinge, die verglichen werden, denselben Objekttyp haben? – austinthesing

+5

@austinthesing bedeutet, dass '42.0 == 42'' true' und '42.0.eql erzeugt? 42 'erzeugt "falsch". –

Verwandte Themen