【问题标题】:Zend Framework 2 database testing - canCallMagicCall() on a non objectZend Framework 2 数据库测试 - 非对象上的 canCallMagicCall()
【发布时间】:2013-04-01 07:53:32
【问题描述】:

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

你有什么提示可能存在错误吗?

【问题讨论】:

  • 请发布完整的堆栈跟踪
  • 我已将其添加到原帖中。
  • 可能是显而易见的,但您是否为BookTable 使用了正确的类,例如use BookModule\Model\BookTable;?能发一下测试类的top吗?
  • 是的,我使用的是正确的导入 :) 问题已在下面解决。感谢您的努力:)

标签: php unit-testing phpunit zend-framework2


【解决方案1】:

你确定你调用的方法是正确的就行了:

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

据我在 Zend 文档中看到的,它应该是 expects()。如果是这种情况,请尝试让我知道。

【讨论】:

  • 天哪,我完全没看到!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
相关资源
最近更新 更多