单元测试将异常呈现到 HTTP 响应中?

unit test render an exception into an HTTP response?

如何测试 render() HTTP 响应异常是否正常工作。我想在不调用 $this->get()

的情况下进行测试

这是Laravel中的一个方法:

  public function render($request, Exception $e)
  {
        $response['exception'] = get_class($e);
        $response['message'] = $e->getMessage();

        if ($e instanceof LockException) {
            return $this->errorResponse('lock', $response, 'Lock error has occurred', $e->getCode());
        }

        return parent::render($request, $e);
    }

我需要测试 LockException 是否已经变成 HTTP 响应。

像这样。在您的测试中,将控制器实例化为一个变量,创建一个空白请求并传入您的 LockException:

$response = $controller->render($request, $exception);
$this->assertEquals('string of HTML you expect', $response);