0
請考慮此功能使用嘲笑的對象,結果爲嘲笑功能
public static function load($ID, $iConnectionType=NULL)
{
$oStatement = self::getLoadStatement($ID);
if ($oStatement->rowCount() == 0) {
throw new \ObjectNotFoundException($ID, 'Unable to load asset');
}
$oItem = self::loadFromStatement($oStatement);
$oItem->setConnectionType($iConnectionType);
return $oItem;
}
現在我想測試這個功能。
我至今是:
public function testLoad()
{
$oMockedPDOStatement = $this->getMockBuilder('\PDOStatement')
->setMethods(array('rowCount'))
->getMock();
$oMockedPDOStatement->expects($this->any())->method('rowCount')->will($this->returnValue(1));
$mockedObject = $this->getMockBuilder('AssetFactory')
->setMethods(array('loadFromStatement', 'getLoadStatement'))
->getMock();
$mockedObject::staticExpects($this->any())
->method('getLoadStatement')
->will($this->returnValue($oMockedPDOStatement));
$mockedObject::staticExpects($this->any())
->method('loadFromStatement')
->will($this->returnValue(new TestAsset()));
$this->assertInstanceOf('TestAsset', $mockedObject::load(5));
}
問題是運行這個測試,我得到以下故障時
1)AssetFactoryTest :: testLoad ObjectNotFoundException:無法加載資產
我認爲oMockedPDOStatement
會照顧$oStatement->rowCount()
,但顯然情況並非如此。什麼是解決方案來正確測試這個?
謝謝。