断言HTTP状态代码为200而不是500失败 [英] Failed asserting the HTTP status code is 200 not 500
问题描述
我正在尝试功能测试某个请求的HTTP状态代码是200
而不是500
.我正在使用Symfony2,这是代码:
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());
}
结果:
1)Ibw \ JobeetBundle \ Tests \ Functional \ JobControllerTest :: testIndex 无法断言500个匹配预期的200个.
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.
那这里怎么了?
推荐答案
您遇到的任何情况都可能导致500错误.
The 500 error can be caused by anything in your case.
在这种情况下,您可以做的是转储响应的内容并检查Symfony调试消息.
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();
}
}
其中div.text_exception > h1
是从Symfony2调试页面到消息本身的xpath
where div.text_exception > h1
is the xpath from Symfony2 debug page to the message itself
然后只打印$error
这篇关于断言HTTP状态代码为200而不是500失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!