使用 PHPUnit 区分 NULL 和 FALSE
有谁知道用 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.
Does anyone know a reliable way to distinguish between FALSE and NULL with PHPUnit?
I'm trying to distinguish from NULL and FALSE in return values in my assertions.
This fails:
$this->assertNotEquals(FALSE, NULL);
And these assertions pass:
$this->assertFalse(NULL);
$this->assertNull(FALSE);
Edit: For some context, this is to distinguish between an error state (FALSE) versus an empty result (NULL). To ensure the function is returning properly, I need to distinguish between the two.
Thanks
Edit...
As per some of the problems regarding what I am testing, I'm adding the tests.
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");
}
}
And the results.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些断言使用
==
来执行类型强制。 Hamcrest 有identicalTo($value)
,它使用===
,我相信 PHPUnit 有assertSame($expected, $actual)
,它执行相同的。更新:回答您的评论,“它可以是 NULL 或对象”:
使用 Hamcrest 断言更具表现力,特别是在发生故障时:
These assertions use
==
which will perform type coercion. Hamcrest hasidenticalTo($value)
which uses===
, and I believe PHPUnit hasassertSame($expected, $actual)
which does the same.Update: In answer to your comment, "It can be NULL or an object":
Using Hamcrest assertions is a little more expressive, especially in the event of a failure:
自己执行比较,并使用严格类型运算符。
http://php.net/operators.comparison
Execute the comparison yourself, and use the strict type operator.
http://php.net/operators.comparison
@David 的assertSame (+1) 是正确的,它会为你做 === 严格的比较。
但我问你:
你使用的是哪个版本的 phpunit?
此断言:
应该产生错误!
示例代码:
结果:
@David is right with assertSame (+1), it will do === strict comparison for you.
But let me ask you:
Which version of phpunit are you using?
This assertion:
should produce and error!
Sample Code:
Results in: