Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop phpunit:模拟对象不会愚弄php_Oop_Unit Testing_Zend Framework2_Phpunit - Fatal编程技术网

Oop phpunit:模拟对象不会愚弄php

Oop phpunit:模拟对象不会愚弄php,oop,unit-testing,zend-framework2,phpunit,Oop,Unit Testing,Zend Framework2,Phpunit,我正在使用phpunit测试一个带有模拟对象的类。当我将从Zend\Db\TableGateway创建的模拟传递给我的类(谁的构造函数需要Zend\Db\TableGateway)时,我得到一个类型错误: "...Argument 1 passed to Album\Model\AlbumTable::__construct() must be an instance of Zend\Db\TableGateway\TableGateway, instance of Mock_TableGate

我正在使用phpunit测试一个带有模拟对象的类。当我将从Zend\Db\TableGateway创建的模拟传递给我的类(谁的构造函数需要Zend\Db\TableGateway)时,我得到一个类型错误:

"...Argument 1 passed to Album\Model\AlbumTable::__construct() must be an instance of Zend\Db\TableGateway\TableGateway, instance of Mock_TableGateway_65b55cb0 given..."
这应该发生吗?phpunit模拟对象是否应该能够“愚弄”类

下面是真正的课堂:

class AlbumTable {
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway) {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll() {
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }

    public function getAlbum($id){
        $id = (int) $id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();
        if(!$row) {
            throw new \Exception("Couldn't find row: $id");
        }
        return $row;
    }

    public function saveAlbum(Album $album) {
        $data = array(
            'artist' => $album->artist,
            'title' => $album->title,
        );

        $id = (int)$album->id;
        if ($id == 0) {
            $this->tableGateway->insert($data);
        } else {
            if ($this->getAlbum($id)) {
                $this->tableGateway->update($data, array('id' => $id));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }

    public function deleteAlbum($id) {
        $this->tableGateway->delete(array('id' => $id));
    }

}
以及测试:

class AlbumTableTest extends PHPUnit_Framework_TestCase {
    public function testFetchAllReturnsAllAlbums() {
        $resultSet = new ResultSet();
        $mockTableGateway = $this->getMock('Zend\Db\TableGateway',
            array('select'), array(), '', false);

        $mockTableGateway->expects($this->once())
            ->method('select')
            ->with()
            ->will($this->returnValue($resultSet));

        $albumTable = new AlbumTable($mockTableGateway);
        $this->assertSame($resultSet, $albumTable->fechAll());
    }
}
错误是:

Time: 102 ms, Memory: 5.00Mb

There was 1 error:

1) AlbumTest\Model\AlbumTableTest::testFetchAllReturnsAllAlbums
Argument 1 passed to Album\Model\AlbumTable::__construct() must be an instance of Zend\Db\TableGateway\TableGateway, instance of Mock_TableGateway_65b55cb0 given, called in C:\Users\MEEE\Google Drive\code\iis\www\CommunicationApp\module\Album\test\AlbumTest\Model\AlbumTableTest.php on line 20 and defined

C:\Users\MEEE\Google Drive\code\iis\www\CommunicationApp\module\Album\src\Album\Model\AlbumTable.php:9
C:\Users\MEEE\Google Drive\code\iis\www\CommunicationApp\module\Album\test\AlbumTest\Model\AlbumTableTest.php:20

FAILURES!
Tests: 4, Assertions: 9, Errors: 1.

你没有嘲笑正确的类。您正在创建
Zend\Db\TableGateway
的模拟,需要实际模拟
Zend\Db\TableGateway\TableGateway

将测试代码更改为:

    $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
        array('select'), array(), '', false);
您的模拟未能通过类型提示,因为您没有模拟正确的类


模拟对象将扩展您正在模拟的类,因此它们将是被模拟的类的实例。

创建模拟后,执行var\u转储以查看instanceof是否返回true。。。变量转储($mockTableGateway instanceof Zend\Db\TableGateway);