devlop / phpunit-exception-assertions
PHPUnit 异常断言。
1.1.1
2021-03-05 08:28 UTC
Requires
- php: ^7.4|^8.0
- phpunit/phpunit: ^9.5
Requires (Dev)
- symfony/var-dumper: ^5.2
This package is auto-updated.
Last update: 2024-09-05 16:37:56 UTC
README
PHPUnit 异常断言
包含断言以便更容易测试抛出(或未抛出)的异常的特质。
安装
composer require --dev devlop/phpunit-exception-assertions
用法
包含该特质以获取对断言的访问权限。
use Devlop\PHPUnit\ExceptionAssertions; class YourTest extends TestCase { use ExceptionAssertions; }
可用断言
assertExceptionThrown
断言在回调执行期间抛出了特定的异常,如果回调在未抛出异常的情况下完成,则断言失败。
要检查任何异常,请使用 \Throwable::class
作为第一个参数,因为所有异常都继承自 \Throwable
。
$this->assertExceptionThrown(\InvalidArgumentException::class, function () : void { // code that should throw the expected exception });
assertExceptionNotThrown
断言在回调执行期间未抛出特定的异常,如果在执行期间抛出了指定的异常,则断言失败。
谨慎使用 - 这只会断言未抛出特定的异常,并且对于抛出的任何其他异常,断言将通过,无论是故意的还是意外的。在大多数情况下,可能最好使用 assertNoExceptionsThrown
。
$this->assertExceptionNotThrown(\InvalidArgumentException::class, function () : void { // code that should not throw the exception });
assertNoExceptionsThrown
断言在回调执行期间未抛出任何异常,如果在执行期间抛出了任何异常,则断言失败。
$this->assertNoExceptionsThrown(function () : void { // code that should not throw any exceptions });