【发布时间】:2011-08-23 18:51:49
【问题描述】:
有谁知道用 PHPUnit 区分 FALSE 和 NULL 的可靠方法吗?
我试图在我的断言中区分返回值中的 NULL 和 FALSE。
这失败了:
$this->assertNotEquals(FALSE, NULL);
这些断言通过了:
$this->assertFalse(NULL);
$this->assertNull(FALSE);
编辑:在某些情况下,这是为了区分错误状态 (FALSE) 和空结果 (NULL)。为了确保函数正确返回,我需要区分两者。 谢谢
编辑... 根据我正在测试的一些问题,我正在添加测试。
Class testNullFalse extends PHPUnit_Framework_TestCase{
public function test_null_not_false (){
$this->assertNotEquals(FALSE, NULL, "False and null are not the same");
}
public function test_null_is_false (){
$this->assertFalse(NULL, "Null is clearly not FALSE");
}
public function test_false_is_null (){
$this->assertNull(FALSE, "False is clearly not NULL");
}
public function test_false_equals_null(){
$this->assertEquals(FALSE, NULL, "False and null are not equal");
}
public function test_false_sameas_null(){
$this->assertSame(FALSE, NULL, "False and null are not the same");
}
public function test_false_not_sameas_null(){
$this->assertNotSame(FALSE, NULL, "False and null are not the same");
}
}
还有结果。
PHPUnit 3.5.10 by Sebastian Bergmann.
FFF.F.
Time: 0 seconds, Memory: 5.50Mb
There were 4 failures:
1) testNullFalse::test_null_not_false
False and null are not the same
Failed asserting that <null> is not equal to <boolean:false>.
2) testNullFalse::test_null_is_false
Null is clearly not FALSE
Failed asserting that <null> is false.
3) testNullFalse::test_false_is_null
False is clearly not NULL
Failed asserting that <boolean:false> is null.
4) testNullFalse::test_false_sameas_null
False and null are not the same
<null> does not match expected type "boolean".
FAILURES!
Tests: 6, Assertions: 6, Failures: 4.
【问题讨论】:
-
PHPunit(至少 v3.7+,如果不是之前)现在有 assertSame() 来检查两个变量是否具有相同的类型和值。
标签: php unit-testing null phpunit