Unit testing 调用模拟类的父函数
我有以下问题Unit testing 调用模拟类的父函数,unit-testing,mocking,phpunit,Unit Testing,Mocking,Phpunit,我有以下问题 class A { public function isNew() { return ($this->ID == 0); } } class B extends A { //Some functions } 现在我想模拟B类,所以我有一些语句 $oMockedStm = $this->getMockBuilder('B')->getMock(); $oMocke
class A
{
public function isNew()
{
return ($this->ID == 0);
}
}
class B extends A
{
//Some functions
}
现在我想模拟B类,所以我有一些语句
$oMockedStm = $this->getMockBuilder('B')->getMock();
$oMockedStm->expects($this->any())->method('someMethod')->will($this->returnValue(TRUE));
$oMockedStm->expects($this->any())->method('anotherMethod')->will($this->returnValue(TRUE));
现在当我这么做的时候
$this->assertTrue($oMockedStm->isNew());
我得到一个错误:断言null为true失败
这怎么可能。函数总是返回true或false
这与不能调用模拟对象的父方法有关吗?我发现我不想模拟整个类。只有特定的功能。 因此,在定义模拟对象时,使用
setMethods()
函数指定要模拟的特定函数
所以像这样:
$oMockedStm = $this->getMockBuilder('B')
->setMethods(array('someMethod','anotherMethod'))
->getMock();