相关疑难解决方法(0)

如何在PHPUnit中使用expectException?

我正在尝试测试我的异常,或 PHP 单元中的任何其他异常。

<?php declare(strict_types=1);


namespace Tests\Exception;

use PHPUnit\Framework\TestCase;

class DrinkIsInvalidExceptionTest extends TestCase
{
    public function testIsExceptionThrown(): void
    {
        $this->expectException(\Exception::class);
        try {
            throw new \Exception('Wrong exception');
        } catch(\Exception $exception) {
            echo $exception->getCode();
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

仍然失败:

Failed asserting that exception of type "Exception" is thrown.
Run Code Online (Sandbox Code Playgroud)

可能是什么问题呢?

php testing phpunit exception

7
推荐指数
1
解决办法
2963
查看次数

标签 统计

exception ×1

php ×1

phpunit ×1

testing ×1