2015-10-03 12 views
17

Sự khác nhau giữa việc sử dụng eqeql trong các thử nghiệm rspec là gì? Có một sự khác biệt giữa:Rspec `eq` so với` eql` trong các bài kiểm tra `mong đợi`

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 

và:

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 

Trả lời

23

Có sự khác biệt tinh tế ở đây, dựa vào loại bình đẳng được sử dụng trong so sánh.

Từ các tài liệu 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 sử dụng toán tử == để so sánh, và eql bỏ qua chuyển đổi loại.

+0

Một chuyển đổi loại có nghĩa là nó đang tìm kiếm các đối tượng hoặc vật được so sánh với cùng một loại đối tượng? – austinthesing

+5

@austinthesing nó có nghĩa là '42.0 == 42' tạo' true' và '42.0.eql? 42' tạo 'false'. –

Các vấn đề liên quan