gpt4 book ai didi

php - 如何让 PHPUnit 打印内部异常?

转载 作者:行者123 更新时间:2023-12-05 08:09:05 33 4
gpt4 key购买 nike

有没有办法让 PHPUnit 在出现问题时打印内部异常?

这是一个简单的例子:

class TestTest extends \PHPUnit\Framework\TestCase
{
function test() {
throw new Exception("Outer exception message", 0, new Exception("Inner exception message"));
}
}

我想看到两条消息,但我只看到外面的一条。

最佳答案

  1. 装饰器模式很有帮助。
class OuterDecoratorException extends \Exception {

private $innerException;

public function __construct(string $message, \Exception $innerException) {
$this->message = $message;
$this->innerException = $innerException;
}

public function __toString(): string {
return \sprintf(
"Outer: %s Inner: %s",
$this->getMessage(),
$this->innerException->getMessage()
);
}

}



throw new OuterDecoratorException("Outer message", new \Exception("Inner message"));

<强>2。优先于装饰器,在每个测试中直接编译异常消息。

class TestTest extends \PHPUnit\Framework\TestCase
{
function test() {

throw new \Exception(
sprintf(
"Inner: %s Outer: %s",
"Inner message",
(new \Exception("Outer message"))->getMessage()
),
0
);
}
}
  1. 否则你必须扩展一个库。请参阅手册 - 如何扩展框架。

关于php - 如何让 PHPUnit 打印内部异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41880664/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com