Failed asserting the HTTP status code is 200 not 500

旧时模样 提交于 2019-12-20 02:29:32

问题


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 ?


回答1:


The 500 error can be caused by anything in your case.

What you could do in such situation is to dump the content of the response and check the Symfony debugging message.

I usually use such code snippet for quick checking such errors directly in terminal:

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

where div.text_exception > h1 is the xpath from Symfony2 debug page to the message itself

Then just print the $error




回答2:


You could also echo the html after the request to see what's in the response.

echo $crawler->html();


来源:https://stackoverflow.com/questions/19426364/failed-asserting-the-http-status-code-is-200-not-500

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!