Fixing time inconsistencies in rspec

Posted . Visible to the public.

When comparing a date or time that has come from the database there may be slight differences that are not immediately noticeable. The spec will fail but the times in the output will look the same, this is due to microsecond differences.

One way around this (and please update this card if there is a better way) is to convert the time to an integer before doing the comparison i.e.

Timecop.freeze do
  record = FactoryGirl.create...
  Time.current.to_i.should == record.created_at.to_i
end

Old rspec syntax because i'm working on an oooooold app.

Rob Williams
Last edit
Rob Williams
Posted by Rob Williams to Foxsoft (2015-11-12 12:37)