2013-02-02 57 views
6

我已经使用了来自Zend Framework 2的Album示例文档并创建了一个应用程序。Zend Framework 2 phpunit测试加入的表

现在同时使用phpunit单元测试它,我有一个问题,同时测试其具有与join表说表Account_Type

这里是它的代码。

fetchAll功能是

function fetachAll() 
{ 
    $sql = new Sql($this->tableGateway->getAdapter()); 

    $select = $sql->select(); 

    $select->from('Album') 
      ->columns(array('id', 'name', 'account_type_id', 'managing_account_id')) 
     ->join(array('AT' => 'account_type'), 'album.account_type_id = AT.account_type_id'); 

    $resultSet = $this->tableGateway->selectWith($select); 

    return $resultSet; 
} 

上面表单位测试代码。

public function testFetchAllReturnsAllAlbums() 
{ 
    $resultSet= new ResultSet(); 

    $mockTableGateway = $this->getMock(
     'Zend\Db\TableGateway\TableGateway', 
     array('select'), 
     array(), 
     '', 
     false 
    );  

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

    $albumTable = new AlbumTable($mockTableGateway); 

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

我得到错误此错误

Argument 1 passed to Zend\Db\Sql\Sql::__construct() must be an instance of 
Zend\Db\Adapter\Adapter, null given, 

此行$this->assertSame($resultSet, $albumTable->fetchAll());testFetchAllReturnsAllAlbums方法。

如果任何一个人都做PHPUnit的测试加盟,请提供相同的例子。

+2

要调用'$这个 - > tableGateway-> getAdapter()'你的模拟对象,它返回NULL,并'Zend的\ DB \ SQL \ Sql'预计'Zend的\ DB \适配器\ Adapter'实例。我对mock对象还不是很熟悉,但是你不是在嘲笑'TableGateway'上的'select'方法,但是在你的'AlbumTable'中,你正在''Sql'对象上调用'select'方法吗? – Andy0708

回答

1

您可能想要模拟Zend\Db\TableGateway\TableGateway对象的getAdapter方法。调用此方法并将其返回值传递给Zend\Db\Sql\Sql构造函数。