问题描述
编辑单元测试,我遇到了这个问题。我要测试的一段代码捕获了\Doctrine\DBAL\Exception\RetryableException
。类链中的第一个构造函数是DriverException
的构造函数,其构建方式如下:
/**
* @param string $message The exception message.
* @param \Doctrine\DBAL\Driver\DriverException $driverException The DBAL driver exception to chain.
*/
public function __construct($message,\Doctrine\DBAL\Driver\DriverException $driverException)
{
$exception = null;
if ($driverException instanceof Exception) {
$exception = $driverException;
}
parent::__construct($message,$exception);
$this->driverException = $driverException;
}
我觉得我在这里面临鸡蛋和鸡肉的问题。我怎样才能实例化一个将自身实例作为强制参数的类?
解决方法
注意:我不会将此自动响应标记为解决方案,这是一种解决方法。
我没有在我的单元测试模拟中抛出正确的异常,而是创建了一个更简单的异常,扩展了Exception
,但仍然实现了原始接口RetryableException
,因为它是代码中捕获的接口。我正在测试。虽然不是我想做的,但它确实可以满足我的要求。