rspec-expectations: Better failure message when two unequal objects have the same string representation.

In the following error:

describe 'time' do
  it 'should be equal' do
    expect(Time.now).to eq(Time.now)
  end
end
> rspec repro.rb
F

Failures:

  1) time should be equal
     Failure/Error: expect(Time.now).to eq(Time.now)

       expected: 2013-10-02 19:51:58 -0700
            got: 2013-10-02 19:51:58 -0700

       (compared using ==)

       Diff:
     # ./repro.rb:3:in `block (2 levels) in <top (required)>'

Finished in 0.00273 seconds
1 example, 1 failure

Failed examples:

rspec ./repro.rb:2 # time should be equal

expected and actual are presented as the same, even though they’re not. While we do call out that == was used to compare them, this is still a confusing error. Better would be something like:

   expected: 2013-10-02 19:51:58 -0700 (Object ID 12345)
        got: 2013-10-02 19:51:58 -0700 (Object ID 56789)

Relevant code is https://github.com/rspec/rspec-expectations/blob/master/lib/rspec/matchers/built_in/eq.rb

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 18 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Isn’t the issue here that the nanoseconds are different on the two time objects? Time instances do support equality, if you do Time.at(0) == Time.at(0) it returns true. Returning the object id makes it seem like that’s why they’re unequal.