您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit断言assertClassHasAttribute
发布时间:2021-09-30 23:02:49编辑:雪饮阅读()
Phpunit断言assertClassHasAttribute用于断言一个类包含某个属性,若该属性存在,则断言成功。
ClassHasAttributeTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class ClassHasAttributeTest extends TestCase
{
public $a="c";
public function testFailure(): void
{
$this->assertClassHasAttribute('foo', stdClass::class);
}
public function testFailure2(): void
{
$this->assertClassHasAttribute('a', ClassHasAttributeTest::class);
}
}
use PHPUnit\Framework\TestCase;
final class ClassHasAttributeTest extends TestCase
{
public $a="c";
public function testFailure(): void
{
$this->assertClassHasAttribute('foo', stdClass::class);
}
public function testFailure2(): void
{
$this->assertClassHasAttribute('a', ClassHasAttributeTest::class);
}
}
像这里第一个当然是断言失败,stdClass类中怎么可能会有foo这样普通的属性。那么第二个断言断言当前类中有a属性,则是复合实际的。
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\ClassHasAttributeTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
F. 2 / 2 (100%)
Time: 00:00.013, Memory: 6.00 MB
There was 1 failure:
1) ClassHasAttributeTest::testFailure
Failed asserting that class "stdClass" has attribute "foo".
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\ClassHasAttributeTest.php:9
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
关键字词:phpunit,assertClassHasAttribute