RSpec comparison of datetime objects failing


#1

Comparison of DateTime objects in nested data structures is failing when the objects are equal. Two separate tests that were comparing hashes that had DateTime objects as values both failed on CircleCI, but passed locally (on multiple developer machines).

We are using:
rspec (3.2.0)
rails (4.2.1)
ruby 2.1.6

Examples of the two tests:

expect({ start_date: <A DateTime Object> }).to include({ start_date: <Another DateTime object> })

expect([ { start_date: <A DateTime Object> }, { start_date: <A DateTime Object> }]).to(
  match_array([ { start_date: <A DateTime Object> }, { start_date: <A DateTime Object> }]
)

Both of these tests passed in CircleCI when to_s was called on the DateTime objects.


#2

We came across this same issue.

We are using:
rspec (3.5.2)
rails (5.0.1)
ruby (2.3.0)

Ended up updating the tests to compare the specific DateTime object details that are important for us through .strftime.


#3