php - Zend Framework 2 数据库测试 - 非对象上的 canCallMagicCall()

标签 php unit-testing phpunit zend-framework2

我一直在尝试使用本教程测试我的模型表: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;
}

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

最佳答案

您确定您所调用的方法正确吗:

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

从Zend文档中我可以看到,它应该是expects()。如果是这种情况,请尝试告诉我。

关于php - Zend Framework 2 数据库测试 - 非对象上的 canCallMagicCall(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15909491/

相关文章:

php - 是否可以在 php 中传递像 myfunction($var1 ="hi") 这样的变量?

php - 在 yii 中为小部件创建测试

java - hibernate spring简单单元测试读取数据。 findAll() 无法引用

php - 如何使用 PHPUnit 对并发读/写进行单元测试?

php - 将 PHPUnit 与 Selenium 结合使用,如何测试元素是否包含某些内容?

php - 防止 iframe 窃取

php - symfony 通知 : Undefined index: username

ruby - 确保拆卸在 Test::Unit::TestCase 中运行?

javascript - 使用 Jasmine 测试 Angular Promise

phpunit - Laravel 4 在测试用例中找不到测试