2012-07-24 74 views
0

我跟在Rob Allens ZF 1 Tutorial之後,並希望用一些UnitTesting進行皮條客。但每當我運行PHPUnit的命令,我得到的消息:

here was 1 failure: 

1) IndexControllerTest::testDeleteAction 
Failed asserting last controller used <"error"> was "Index" 

/path/to/library/Zend/Test/PHPUnit/ControllerTestCase.php:1000 
/path/to/tests/application/controllers/IndexControllerTest.php:55 

FAILURES! 
Tests: 4, Assertions: 9, Failures: 1. 

有問題的行動是deleteAction,看起來像這樣:

public function deleteAction() { 
    if ($this->getRequest()->isPost()) { 
     $del = $this->getRequest()->getPost('del'); 
     if ($del == 'Yes') { 
      $id = $this->getRequest()->getPost('id'); 
      $wishes = new Application_Model_DbTable_Wishes(); 
      $wishes->deleteWish($id); 
     } 
     $this->_helper->redirector('index'); 
    } 
    else { 
     $id = $this->_getParam('id', 0); 
     $wishes = new Application_Model_DbTable_Wishes(); 
     $this->view->wish = $wishes->getWish($id); 
    } 
} 

我跟蹤誤差下拉至$wishes>getWish($id);所以,如果我去那個功能,看起來像這樣:

public function getWish($id) { 
    $id = (int) $id; 
    $row = $this->fetchRow('id = ' . $id); 
    if(!$row){ 
     throw new Exception("Could not find row $id"); 
    } 
    return $row->toArray(); 
} 

它出現行$row = $this->fetchRow('id = ' . $id);導致問題。我無法弄清楚爲什麼。所有的行動工作都很好,他們按預期行事。任何想法如何解決這個問題?

謝謝!

回答

0

也許嘗試使用選擇()對象,而不是當一個普通的字符串:

public function getWish($id) { 
    $id = (int) $id; 
    $select = $this->select(); 
    $select->where('id = ?', $id); 
    $row = $this->fetchRow($select); 
    if(!$row){ 
     throw new Exception("Could not find row $id"); 
    } 
    return $row->toArray(); 
} 

這只是胡亂猜測,但誰知道。唯一看起來很奇怪的是在查詢字符串(?)中缺少佔位符。

FetchRow()不一樣,如果你傳遞一個字符串的第一件事fetchRow與選擇()對象的工作,實際上是()做的是建立一個選擇()。所以也許它只是不喜歡字符串。