我在 PHPUnit 中使用 assertSame() 将数据库结果与预期值进行比较。结果是 float 。
PHPUnit 返回此消息(但我无法发现任何差异):
Failed asserting that Array (
'1_1' => 11.111111111111
'1_2' => 33.333333333333
'1_3' => 55.555555555556
'1_4' => 0.0
'1_5' => null
'1_total' => 100.0
) is identical to Array (
'1_1' => 11.111111111111
'1_2' => 33.333333333333
'1_3' => 55.555555555556
'1_4' => 0.0
'1_5' => null
'1_total' => 100.0
)
为什么会失败?比较浮点值数组的正确方法是什么?
最佳答案
assertEquals 对于这种情况有一个 $floating_delta 参数:
$this->assertEquals($expected_array, $actual_array, '', 0.00001);
关于php - 浮点测试断言 - 为什么这些 "identical"数组会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23734857/