PHPUnit assertNull() 测试失败

标签 php phpunit assertions

我确定我测试的数组为空。即使执行 var_dump(array) 也会打印 array(0) { }

但是测试 $this->assertNull($array); 失败。
相反,当我测试下面的代码时,它进入 if 条件:

if ($array == null) {
    echo "Entered";
} else {
    echo "Not Entered";
}

我不明白为什么会这样。如果有人知道原因请解释一下。

最佳答案

array(0) { } 是一个空数组。

null 表示根本没有数组。

它们不是同一件事。

== 的问题在于它试图输入不同的值来匹配它们。空数组是“falsy”,null 也是如此。

如果您想看到差异,请使用 === 代替,它不会输入 juggle 并且还会比较类型;

$array1 = null;
$array2 = array();
if ($array1 == null) echo '1';     // $array1 is "similar to" null.
if ($array1 === null) echo '2';    // $array1 is null
if ($array2 == null) echo '3';     // $array2 is "similar to" null
if ($array2 === null) echo '4';    // $array2 is null

>>> 123

More on the comparison operators here .

关于PHPUnit assertNull() 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17911082/

相关文章:

mysql - 如何编写失败的 SQL 查询?

php - iCal 不显示组织者的 session 时间段

php - 在 URL 查询中表示约 200 个 bool 标志

php - 如何禁用缓存 100%

javascript - 仅在模态打开时加载 iframe src

php - 如何在 php 中更改单元测试日期函数的当前时间

php - PHPUnit 中的测试方法应该如何命名

phpunit - Netbeans-PHPUnit '' 无法识别的选项 --run''

arrays - 在 Protractor 中断言数组

hardware - 将 VHDL 监视器转换为 PSL 断言