2

我一直在尝试使用本教程测试我的模型表:http: //framework.zend.com/manual/2.1/en/tutorials/unittesting.html

我尝试将它们应用到我自己的应用程序中,实际上它与教程中的专辑非常相似,只是做了一些小的修改。

当我尝试运行 fetchAll() 测试时,出现以下错误:

PHP Fatal error:  Call to a member function canCallMagicCall() on a non-object in C:\Program Files (x86)\Zend\Apache2\htdocs\ukazka2\vendor\zendframework\zendframework\library\Zend\Db\TableGateway\AbstractTableGateway.php on line 470

堆栈跟踪:

[09-Apr-2013 21:23:19 UTC] PHP Stack trace:
[09-Apr-2013 21:23:19 UTC] PHP   1. {main}() C:\Program Files (x86)\Zend\ZendServer\bin\phpunit:0
[09-Apr-2013 21:23:19 UTC] PHP   2. PHPUnit_TextUI_Command::main() C:\Program Files (x86)\Zend\ZendServer\bin\phpunit:46
[09-Apr-2013 21:23:19 UTC] PHP   3. PHPUnit_TextUI_Command->run() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\TextUI\Command.php:129
[09-Apr-2013 21:23:19 UTC] PHP   4. PHPUnit_TextUI_TestRunner->doRun() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\TextUI\Command.php:176
[09-Apr-2013 21:23:19 UTC] PHP   5. PHPUnit_Framework_TestSuite->run() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\TextUI\TestRunner.php:349
[09-Apr-2013 21:23:19 UTC] PHP   6. PHPUnit_Framework_TestSuite->run() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestSuite.php:705
[09-Apr-2013 21:23:19 UTC] PHP   7. PHPUnit_Framework_TestSuite->runTest() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestSuite.php:745
[09-Apr-2013 21:23:19 UTC] PHP   8. PHPUnit_Framework_TestCase->run() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestSuite.php:775
[09-Apr-2013 21:23:19 UTC] PHP   9. PHPUnit_Framework_TestResult->run() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestCase.php:776
[09-Apr-2013 21:23:19 UTC] PHP  10. PHPUnit_Framework_TestCase->runBare() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestResult.php:648
[09-Apr-2013 21:23:19 UTC] PHP  11. PHPUnit_Framework_TestCase->runTest() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestCase.php:831
[09-Apr-2013 21:23:19 UTC] PHP  12. ReflectionMethod->invokeArgs() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestCase.php:976
[09-Apr-2013 21:23:19 UTC] PHP  13. BookTest\Model\BookTableTest->testFetchAllReturnsAllBooks() C:\Program Files (x86)\Zend\ZendServer\bin\PEAR\PHPUnit\Framework\TestCase.php:976
[09-Apr-2013 21:23:19 UTC] PHP  14. Mock_TableGateway_26882600->expect() C:\Program Files (x86)\Zend\Apache2\htdocs\ukazka2\module\Book\test\BookTest\Model\BookTableTest.php:15
[09-Apr-2013 21:23:19 UTC] PHP  15. Zend\Db\TableGateway\AbstractTableGateway->__call() C:\Program Files (x86)\Zend\Apache2\htdocs\ukazka2\module\Book\test\BookTest\Model\BookTableTest.php:15

我的测试代码如下:

public function testFetchAllReturnsAllBooks(){
    $resultSet = new ResultSet();
    $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
            array('select'), array(), '', false);
    $mockTableGateway->expect($this->once())
                     ->method('select')
                     ->with()
                     ->will($this->returnValue($resultSet));

    $bookTable = new BookTable($mockTableGateway);

    $this->assertSame($resultSet, $bookTable->fetchAll());
}

这是我的 fetchAll() 函数:

public function fetchAll(){
    if(($resultSet = $this->cache->getItem('books')) == FALSE){
        $resultSet = $this->tableGateway->select();
        $resultSet = $resultSet->toArray();
        $this->cache->setItem('books', $resultSet);         
    }
    $books = array();
    $hydrator = new Hydrator\ArraySerializable();
    foreach($resultSet as $result){
        $books[] = $hydrator->hydrate($result, new Book());
    }
    return $books;
}

您是否有任何提示该错误可能存在于何处?

4

1 回答 1

2

你确定你调用的方法是正确的:

$mockTableGateway->expect($this->once())

从我在 Zend 文档中可以看到,它应该是expects(). 请尝试让我知道是否是这种情况。

于 2013-04-09T21:38:23.817 回答