您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit断言-assertEquals-对象比较
发布时间:2021-10-02 14:48:21编辑:雪饮阅读()
上篇了解了assertEquals对于特定对象如xml对象的DOMDocument实例可以更加专业的去比较xml的文档是否相同。
那么对于普通的对象数据类型来说,其也能对参与断言的预期对象与实际对象进行属性之间的比较,两个对象只要属性上有异同,则就认定断言预期对象与实际对象不同,即便是两次new的,若用php传统知识观念来看,就算两个对象属性什么都是相同的,但是由于是new的两次就应该不属于同一个对象,但是这里并不是断言是否同一个对象,而是断言这两个对象的一致性(除了对象地址)。
EqualsTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class EqualsTest extends TestCase
{
public function testFailure(): void
{
$expected = new stdClass;
$expected->foo = 'foo';
$expected->bar = 'bar';
$actual = new stdClass;
$actual->foo = 'bar';
$actual->baz = 'bar';
$this->assertEquals($expected, $actual);
}
public function testSuccess(): void
{
$expected = new stdClass;
$expected->foo = 'foo';
$expected->bar = 'bar';
$actual = new stdClass;
$actual->foo = 'foo';
$actual->bar = 'bar';
$this->assertEquals($expected, $actual);
}
}
use PHPUnit\Framework\TestCase;
final class EqualsTest extends TestCase
{
public function testFailure(): void
{
$expected = new stdClass;
$expected->foo = 'foo';
$expected->bar = 'bar';
$actual = new stdClass;
$actual->foo = 'bar';
$actual->baz = 'bar';
$this->assertEquals($expected, $actual);
}
public function testSuccess(): void
{
$expected = new stdClass;
$expected->foo = 'foo';
$expected->bar = 'bar';
$actual = new stdClass;
$actual->foo = 'foo';
$actual->bar = 'bar';
$this->assertEquals($expected, $actual);
}
}
运行结果:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe C:\Users\Administrator\PhpstormProjects\untitled\vendor\phpunit\phpunit\phpunit -c C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\EqualsTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
F. 2 / 2 (100%)
Time: 00:00.011, Memory: 4.00 MB
There was 1 failure:
1) EqualsTest::testFailure
Failed asserting that two objects are equal.
--- Expected
+++ Actual
@@ @@
stdClass Object (
- 'foo' => 'foo'
- 'bar' => 'bar'
+ 'foo' => 'bar'
+ 'baz' => 'bar'
)
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\EqualsTest.php:16
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
这里第一个断言自然是两个不同的对象,而第二个断言是两个”相同”的对象。
关键字词:phpunit,assertEquals
相关文章
- phpunit断言-assertEquals-特定类型比较
- phpunit断言-assertEquals
- phpunit断言-assertEmpty
- phpunit断言-assertDirectoryIsWritable
- phpunit断言-assertDirectoryIsReadable
- phpunit断言-assertDirectoryExists
- phpunit断言assertCount
- phpunit断言assertContainsOnlyInstancesOf
- phpunit断言-assertContainsOnly
- phpunit断言assertStringContainsStringIgnoringCase