我遵循Rob Allens ZF 1 教程并想用一些单元测试来改进它。但是每当我运行 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.
有问题的 Action 是 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);
会导致问题。我不知道为什么。所有动作都很好,他们按预期工作。知道如何解决这个问题吗?
谢谢!