2013-02-02 8 views
6

私はZend Framework 2 Documentationのアルバムの例を使用してアプリケーションを作成しました。Zend Framework 2 phpunit joinでテーブルをテストする

phpunitを使用してunittestingしていますが、テーブルAccount_Typejoinのテーブルをテストしているときに問題が発生しています。

ここにそのコードを示します。

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()); 
} 

私は、このエラーtestFetchAllReturnsAllAlbums方法でこのライン$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, 

をエラーを取得しています。

誰かが参加のためのphpunitテストを行っている場合は、その例を挙げてください。

+2

あなたのモックオブジェクトで '$ this-> tableGateway-> getAdapter()'を呼び出して、NULLを返します。 'Zend \ Db \ Sql \ Sql'は' Zend \ Db \ Adapter \ Adapter'のインスタンスを期待しています。 。私はモックオブジェクトにはまだ慣れていませんが、 'TableGateway'で' select'メソッドを嘲笑しているわけではありませんが、 'AlbumTable'では' Sql'オブジェクトで 'select'メソッドを呼び出していますか? – Andy0708

答えて

1

Zend\Db\TableGateway\TableGatewayオブジェクトのgetAdapterメソッドをモックしたい場合があります。このメソッドが呼び出され、その戻り値がZend\Db\Sql\Sqlコンストラクタに渡されます。

関連する問題