How can I make this Time assertion match perfectly with either Time, Date, or DateTime

Viewed 4603

I have an assertion in a test that looks like this:

assert_equals object.sent_at, Time.now

When I run this test, I keep getting an error that looks like this

--- expected +++ actual @@ -1 +1 @@ -Fri, 04 Mar 2016 18:57:47 UTC +00:00 +Fri, 04 Mar 2016

I've tried a few combinations to make this test pass.

My actual code updates the sent_at value with a Time.now but its not quite in the perfect format. It is close but not enough to pass. How can I make this test pass.

Here are some combinations I've tried in my assertions:

Time.now.utc Date.today Time.now

and a lot of to_time , to_datetime etc. How can I make the test pass?

4 Answers

Old but still valid... The output shows that the comparison is against UTC which would be Time.current

At this time you would probably use:

assert_in_delta object.sent_at, Time.current, 1

To tolerate <1 second difference

Related