Why do the following differ?
Time.now.end_of_day == Time.now.end_of_day - 0.days # false
Time.now.end_of_day.to_s == Time.now.end_of_day - 0.days.to_s # true
Because the number of nanoseconds is different:
ruby-1.9.2-p180 :014 > (Time.now.end_of_day - 0.days).nsec
=> 999999000
ruby-1.9.2-p180 :015 > Time.now.end_of_day.nsec
=> 999999998
Like Mischa said, the times differ by nanoseconds. Here is an article on workarounds and fixes for doing this in Rails, specifically for tests like you are doing.
The seemingly most straightforward approach given is to round the times to seconds by appending .to_i
, but there are other alternatives.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With