Phpunit: assertEquals(): NULL, true 和空字符串是一样的

创建于 2013-09-15  ·  3评论  ·  资料来源: sebastianbergmann/phpunit

当我在类 PHPUnit_Framework_Testcase 中使用方法 assertEquals() 并进行比较时:

_false_ 反对 _NULL_,
_空字符串_反对_false_,
_空字符串_反对_NULL_

所有测试都成功通过。 但是没有任何值是相等的。 所以 assertEquals 与“==”运算符进行比较,而不是“===”或类似的东西。 认为它应该是这样的。 但为什么?

最有用的评论

我们有assertSame()用于===比较。

所有3条评论

我们有assertSame()用于===比较。

好吧完美。 这些名字有点误导。 但这是我的错。 对不起。

但是assertSame()只会比较true ,如果对象相同。

class Foo {}
$this->assertSame(new Foo(), new Foo());
Failed asserting that two variables reference the same object.

我需要一些可以像这样工作的代码:

$this->assertSame(new Foo(null), new Foo("")); // false

$this->assertSame(new Foo(null), new Foo(null)); // true
$this->assertSame(new Foo(""), new Foo("")); // true
此页面是否有帮助?
0 / 5 - 0 等级