2017-09-14 47 views
0

如何测试render() HTTP响应中的异常是否正常工作。我想没有调用$this->get()单元测试在HTTP响应中呈现异常?

这是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

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

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

如何实例化一个'$ controller'?从 –

+1

声明哪里同样你可以实例化任何对象! '$ controller = new Whatever($ possiblePassingSomethingItNeedsToo);' – delboy1978uk

+0

哦,那大声笑。我需要找到一种方法来模拟'\ Illuminate \ Http \ Request'来传递给'渲染器' –