在我的Zend Framework項目中,我有一個我正在測試的表單。在我的表單中,多選元素從模型中獲取選項,它從數據庫中檢索它們。Zend框架/ PHPUnit:如何存根/模擬連接到數據庫的對象方法?
public function init()
{
$this->addElement('select', 'Region_ID', array('multiOptions' => $this->getRegions()));
}
protected function getRegions()
{
$mapper = new Model_RegionMapper();
return $mapper->getFormSelectData(); //this method will try to connect to a database (or get another object that will connect to the database)
}
我試圖複製PHPUnit文檔中的示例,但它似乎並沒有工作。
public function setUp()
{
$stub = $this->getMock('Model_RegionMapper');
$stub->expects($this->any())
->method('getFormSelectData')
->will($this->returnValue(array('testdata')));
}
public function testFoo()
{
//this will fail
$form = new My_Form();
}
測試失敗,因爲它試圖在數據庫中查找不存在的表。但我不希望它連接到數據庫。我如何正確存根/模擬此方法,以便它不調用數據庫?