Rounding seems risky as you don't know if it will round up or down.
You could format to the same as your database?
$this->assertEquals(now()->addDays(14)->format('Y-m-d H:i:s'), $user->some_date);
but it seems odd that this should be necessary?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In one of my models I'm casting to a Carbon instance.
protected $dates = [
'some_date',
];
Here's my test:
public function it_saves_the_correct_datetime()
{
$user = User::create();
$this->assertEquals(now()->addDays(14), $user->some_date);
}
This test will fail because the first instance has milliseconds and the instance coming from the database has not.
Failed asserting that two DateTime objects are equal.
--- Expected
+++ Actual
@@ @@
-2020-05-15T15:00:07.305878+0000
+2020-05-15T15:00:07.000000+0000
To fix this I add this at the beginning of the test Carbon::setTestNow(now()->roundSecond()).
Anyone know a better way to compare two Carbon instance to each other without looking at the milliseconds?
Please or to participate in this conversation.