2017-01-23 10 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(たとえば)である必要があります。別のチェックは、応答に例外メッセージが含まれている可能性があります。

関連する問題