2010-12-16 142 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()); 

該消息將只顯示並給我一個線索怎麼回事。使代碼更清潔:)