0

我遵循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);会导致问题。我不知道为什么。所有动作都很好,他们按预期工作。知道如何解决这个问题吗?

谢谢!

4

1 回答 1

0

如果是纯字符串,则可以尝试使用select()对象:

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()确实喜欢使用select()对象,事实上,如果您传递一个字符串,那么 fetchRow() 所做的第一件事就是构建一个select()。所以也许它只是不喜欢这个字符串。

于 2012-07-26T09:30:07.000 回答