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

时间:2017-09-14 12:55:13

标签: php laravel phpunit laravel-5.4

如何测试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响应。

1 个答案:

答案 0 :(得分:1)

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

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