您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit断言-assertInstanceOf
发布时间:2021-10-03 12:19:14编辑:雪饮阅读()
assertInstanceOf断言用来断言一个实际实例的直属类是一个预期类。
InstanceOfTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class InstanceOfTest extends TestCase
{
public function testFailure(): void
{
$this->assertInstanceOf(RuntimeException::class, new Exception);
}
public function testSuccess(): void
{
$this->assertInstanceOf(RuntimeException::class, new RuntimeException);
}
}
use PHPUnit\Framework\TestCase;
final class InstanceOfTest extends TestCase
{
public function testFailure(): void
{
$this->assertInstanceOf(RuntimeException::class, new Exception);
}
public function testSuccess(): void
{
$this->assertInstanceOf(RuntimeException::class, new RuntimeException);
}
}
运行结果如:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.4.3nts\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\InstanceOfTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
F. 2 / 2 (100%)
Time: 00:00.006, Memory: 4.00 MB
There was 1 failure:
1) InstanceOfTest::testFailure
Failed asserting that Exception Object (...) is an instance of class "RuntimeException".
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\InstanceOfTest.php:8
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
关键字词:phpunit,assertInstanceOf