0

私はRob Allens ZF 1 チュートリアルに従い、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);が問題を引き起こしているようです。そして、私はその理由を理解できません。すべてのアクションは正常に機能し、期待どおりに機能します。これを修正する方法はありますか?

ありがとう!

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 に答える