3

I'm trying to functional test the HTTP status code for a certain request is 200 not 500. I'm using Symfony2, and here's the code:

public function testIndex()
{
    $client = static::createClient();
    $crawler = $client->request('GET', "/");
    $this->assertEquals('Ibw\JobeetBundle\Controller\JobController::indexAction', $client->getRequest()->attributes->get('_controller'));
    $this->assertEquals(200 , $client->getResponse()->getStatusCode());
    $this->assertEquals(0, $crawler->filter('.jobs td.position:contains("Expired")')->count());
}

And the result :

1) Ibw\JobeetBundle\Tests\Functional\JobControllerTest::testIndex Failed asserting that 500 matches expected 200.

When I access the route "/" manually through the browser it works just fine.

So what's wrong here ?

4

2 回答 2

2

500 错误可能是由您的情况引起的。

在这种情况下你可以做的是转储响应的内容并检查 Symfony 调试消息。

我通常使用这样的代码片段直接在终端中快速检查此类错误:

if (!$response->isSuccessful()) {
    $block = $crawler->filter('div.text_exception > h1');
    if ($block->count()) {
        $error = $block->text();
    }
}

div.text_exception > h1从 Symfony2 调试页面到消息本身的 xpath在哪里

然后只需打印$error

于 2013-10-17T12:14:51.523 回答
0

您还可以在请求后回显 html,以查看响应中的内容。

echo $crawler->html();
于 2017-06-22T12:02:14.320 回答