2017-09-14 5 views
0

render() HTTP応答の例外が正しく機能するかどうかをテストする方法。ユニットテストでHTTP応答に例外がレンダリングされますか?

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レスポンスに変身しているかどうかをテストする必要があります。私は、これはLaravelのメソッドである$this->get()

を呼び出さずにテストしたいです。

答えて

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'を' render'に渡す方法を模索する必要があります –

関連する問題