對於這種情況和測試接口我會寫至少3個測試:
protected setUp() {
$this->_object = $this->getMockForAbstractClass(
'PickupPoint_Abstract', array(), '', false
);
}
public function testInstanceOf() {
$this->assertInstanceOf('PickupPoint_Abstract', $this->_object);
}
public function testMethodsExistance() {
$methods = get_class_methods($this->_object);
$this->assertTrue(in_array('getPickupPoints', $methods));
$this->assertTrue(in_array('getPickupPointDetails', $methods));
$this->assertTrue(in_array('__construct', $methods));
}
public function testMethodCount() {
$methods = get_class_methods($this->_object);
/**
* PHPUnit add seven own methods in 3.6.11 + __clone + count of these methods
*/
$this->assertEquals(11, count($methods));
}
藉助這些測試,你會避免錯別字,檢查所需的方法存及其是否有新的方法將被添加,這個測試將被打破,因爲方法的數量已經改變了,這就是我們想要的行爲。
這對我來說很好。我總是使用這個接口的測試,但我認爲它可以用於抽象類!
這不是一個抽象類,而是一個接口。抽象類定義了兩個方法存根和實現的方法。 – halfdan 2010-09-15 08:51:47
可能的重複http://stackoverflow.com/questions/190295/phpunit-testing-abstract-classes – 2010-09-15 08:52:09