assertSame()
最后更新于:2022-04-01 03:48:09
# assertSame()
>[info] ## assertSame(mixed $expected, mixed $actual[, string $message = ''])
当两个变量 `$expected` 和 `$actual` 的值与类型不完全相同时报告错误,错误讯息由 `$message` 指定。
`assertNotSame()` 是与之相反的断言,接受相同的参数。
`assertAttributeSame()` 和 `assertAttributeNotSame()` 是便捷包装(convenience wrapper),以某个类或对象的某个 `public`、`protected` 或 `private` 属性作为实际值来进行比较。
**Example A.37. assertSame() 的用法**
~~~
<?php
class SameTest extends PHPUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertSame('2204', 2204);
}
}
?>
~~~
~~~
phpunit SameTest
PHPUnit 5.0.0 by Sebastian Bergmann and contributors.
F
Time: 0 seconds, Memory: 5.00Mb
There was 1 failure:
1) SameTest::testFailure
Failed asserting that 2204 is identical to '2204'.
/home/sb/SameTest.php:6
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
~~~
>[info] ## assertSame(object $expected, object $actual[, string $message = ''])
当两个变量 `$expected` 和 `$actual` 不是指向同一个对象的引用时报告错误,错误讯息由 `$message` 指定。
**Example A.38. assertSame() 应用于对象时的用法**
~~~
<?php
class SameTest extends PHPUnit_Framework_TestCase
{
public function testFailure()
{
$this->assertSame(new stdClass, new stdClass);
}
}
?>
~~~
~~~
phpunit SameTest
PHPUnit 5.0.0 by Sebastian Bergmann and contributors.
F
Time: 0 seconds, Memory: 4.75Mb
There was 1 failure:
1) SameTest::testFailure
Failed asserting that two variables reference the same object.
/home/sb/SameTest.php:6
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
~~~