2017-01-23 102 views
1

測試客戶端我試圖在測試情況下延長Symfony\Bundle\FrameworkBundle\Test\KernelTestCase會拋出異常,測試的EventListener爲它增加新的航線。簡化測試將看起來像這樣:添加新路線的Symfony

public function testHandlingException() 
{ 
    $kernel = $this->createKernel(['environment' => 'test', 'debug' => true]); 
    $kernel->boot(); 

    $client = $kernel->getContainer()->get('test.client'); 

    $controller = new class extends Controller { 
     public function testAction() 
     { 
      throw new \Exception(); 
     } 
    }; 

    $route = new Route(
     'route_500', 
     ['_controller' => get_class($controller).'::testAction'] 
    ); 

    $client 
     ->getContainer() 
     ->get('router') 
     ->getRouteCollection() 
     ->add('route_500', $route); 

    $this->expectException(\Exception::class); 

    $client->request('GET', '/route_500'); 
} 

它失敗:

Failed asserting that exception of type "Exception" is thrown. 

我怎樣才能在飛所以調用它的工作添加路線?

回答

2

您可以在單元測試中捕獲異常。不在end2end。如果一個GET請求拋出一個異常,你可以得到它:但是你必須知道狀態碼是500(例如)。另一個檢查可能是響應包含異常消息。