When working with RSpec at Chowbus, we get failures like this (especially on our ubuntu CI server) from time to time:
# assuming persons table has created_at and updated_at
person = create(:person)
expect(Person.all).to contain_exactly(person)
This is because database (postgres in our case) and Ruby (ActiveRecord) object attribute keeps different precision of time:
# person is not loaded from database
person.created_at # 1539181279.1897986
# ActiveRecord loads this record from database
Person.first.created_at # 1539181279.1897979
Obviously, comparison by obj id is always more efficient and preferred. However, sometimes that would not be viable. When it's necessary to compare by object equality, we found a very simple solution:
person = create(:person).reload
# since person is now loaded from the database, it has the same time precision
# as objects in Person.all.
expect(Person.all).to contain_exactly(person)