$ this-> fetchRow在Zend框架中的phpunit中创建失败

时间:2012-07-24 11:35:15

标签: zend-framework phpunit

我跟着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);会导致问题。我无法弄清楚原因。所有行动都很好,他们按预期行事。任何想法如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分: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()所做的第一件事就是构建一个选择()的。所以也许它只是不喜欢字符串。