Doing some tests on my Laravel app and am running into intermittent failures due to one of my database columns being defined as a FLOAT. When running a test with assertDatabaseHas() it sometimes fails due to floating point uncertainty.
<?php
namespace Test\Unit;
use App\Foo;
class MyModelTest extends TestCase
{
public function testFooCanBar(): void
{
$stringvalue = "baz";
$floatvalue = 234.281;
$data = [
"name" => $stringvalue,
"data" => $floatvalue,
];
Foo::bar($stringvalue, $floatvalue);
$this->assertDatabaseHas("foos", $data);
}
}
Sometimes result:
Failed asserting that a row in the table [foos] matches the attributes {
"name": "baz",
"data": 234.281
}.
Found similar results: [
{
"name": "baz",
"data": 234.280999999999999995
}
]
I can think of a few ways to work around this (change the column to an INT and multiply by 10^x, or just remove the column from the comparison) but I'm wondering if there are any methods I missed to properly check this. When comparing values directly we can use PHPUnit's assertEqualsWithDelta(); is there anything similar for a database check?