Php assertResponseCode(200)上的Zend Framework单元测试失败
我正在我的Zend Framework应用程序上运行一些单元测试。我无法理解的是,以下测试失败:Php assertResponseCode(200)上的Zend Framework单元测试失败,php,unit-testing,zend-framework,phpunit,Php,Unit Testing,Zend Framework,Phpunit,我正在我的Zend Framework应用程序上运行一些单元测试。我无法理解的是,以下测试失败: public function testCreateFaqItem() { $this->LoginUser(); $this->dispatch('/faq/admin-faq/create'); var_dump($this->getResponse()); $this->assertResponseCode(200); $this
public function testCreateFaqItem()
{
$this->LoginUser();
$this->dispatch('/faq/admin-faq/create');
var_dump($this->getResponse());
$this->assertResponseCode(200);
$this->assertQueryContentContains('h1', 'Create');
$this->assertController('admin-faq');
$this->assertAction('edit');
}
如果在assertResponseCode(200)上,当我移除assertResponseCode(200)时,测试通过,则测试失败。任何帮助都将不胜感激
--编辑--
响应对象转储:
object(Zend_Controller_Response_HttpTestCase)#1130 (8) {
["_body":protected]=>
array(1) {
["default"]=>
string(0) ""
}
["_exceptions":protected]=>
array(0) {
}
["_headers":protected]=>
array(1) {
[0]=>
array(3) {
["name"]=>
string(8) "Location"
["value"]=>
string(13) "/user/profile"
["replace"]=>
bool(true)
}
}
["_headersRaw":protected]=>
array(0) {
}
["_httpResponseCode":protected]=>
int(302)
["_isRedirect":protected]=>
bool(true)
["_renderExceptions":protected]=>
bool(false)
["headersSentThrowsException"]=>
bool(true)
}
谢谢您可以通过转储响应对象并查看标题和正文来调试它
// Within your test, before the assertions
var_dump($this->getResponse());
为了解决登录用户并仍然测试响应代码200的问题,我插入了$this->resetResponse();登录用户后
public function testCreateFaqItem()
{
$this->LoginUser();
$this->resetResponse();
$this->dispatch('/faq/admin-faq/create');
$this->assertResponseCode(200);
$this->assertQueryContentContains('h1', 'Create');
$this->assertController('admin-faq');
$this->assertAction('create');
}
对于任何单元测试功能,最后一个参数始终是一条消息,如果测试失败,将显示该消息。因此,对于本例,不需要执行var_转储。相反,这是我测试响应代码200的方式:
$this->assertResponseCode(200, 'The response code is ' . $this->getResponse()->getHttpResponseCode());
只有在测试失败时才会显示该消息,它会给我一个关于发生了什么的线索。使代码更简洁:)实际响应代码是什么?我发现实际响应代码是302,不知道为什么:(我使用$this->getResponse()->getHeaders(),这在响应对象中没有提供任何其他可能指示其重定向原因的信息?请查看请求对象,确保一切正常。我编辑了它,还更改了测试,另一个将始终出错我意识到抱歉,ircmaxell,下次将这样做:(@ircmaxwell为什么?OP要求任何帮助,FAQ建议投票选出最“有用”的答案。最有用的答案不一定是答案,尤其是当问题使这样的答案几乎不可能时。