2010-12-16 177 views
0

我在我的Zend Framework应用程序上运行一些单元测试。我无法理解的是,下面的测试失败:Zend Framework单元测试失败assertResponseCode(200)

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),测试通过失败。任何帮助都感激不尽。

- 编辑 -

Response对象转储:

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) 
} 

感谢

+0

什么是实际的响应代码? – ircmaxell 2010-12-16 20:19:42

+0

我发现实际的响应代码是302,不知道为什么:( – 2010-12-16 20:36:38

回答

0

要解决登录用户的问题,并仍在测试响应代码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'); 
    } 
0

您可以通过倾销响应对象来看,在标题和正文两调试此。

// Within your test, before the assertions 
var_dump($this->getResponse()); 
+0

我使用$ this-> getResponse() - > getHeaders(),它给了我302 – 2010-12-16 20:37:32

+0

响应对象中没有其他可能表明它重定向的原因?看看请求对象,确保一切看起来都正确 – 2010-12-16 20:39:33

+0

我编辑它,也改变了测试,那其他人会一直错误我意识到 – 2010-12-16 20:49:32

1

对于任何单元测试功能,最后一个参数始终是一条消息,如果测试失败,将显示该消息。所以对于这个例子,不需要做var_dump。相反,这是我测试的200响应代码:如果测试失败

$this->assertResponseCode(200, 'The response code is ' . $this->getResponse()->getHttpResponseCode()); 

该消息将只显示并给我一个线索怎么回事。使代码更清洁:)