【发布时间】:2015-05-07 21:06:34
【问题描述】:
我正在开发一个包并使用 Orchestra/Testbench 包进行单元测试。
我正在尝试编写一个 PHPUnit 测试,以验证在引发异常时响应是否正确。在我的存储库中,我抛出以下异常:
use Acme\Common\Exceptions\ValidationException;
...
throw new ValidationException($validator);
我已经在包的服务提供者中注册了处理程序类:
$this->app->singleton('Illuminate\Contracts\Debug\ExceptionHandler', 'Acme\Common\Exceptions\Handler');
但是 Handler 类中的 render() 方法没有被触发。这是 render() 方法:
public function render($request, Exception $e)
{
if ($e instanceof \Acme\Common\Exceptions\ValidationException) {
$message = implode(' ', array_flatten($exception->getMessages()->toArray()));
$response = array('errorCode' => $exception->getCode());
return \Response::make($response, 400);
}
return parent::render($request, $e);
}
相反,我只得到通用异常方法:
Acme\Common\Exceptions\ValidationException: {"key":["The key field is required."]}
我什至在 render() 方法的开头放了一个 dd() ,但没有。我是否缺少 Orchestra Testbench 的某种设置?
【问题讨论】: