class baseClass
{
public function __construct()
{
}
public function methodToBeMocked()
{
// do somthing
}
}
class childClass extends baseClass
{
public function __construct()
{
parent::__construct($doctrine, $securityContext);
}
public function methodUsingMethodToBeMocked()
{
// do somthing
$this->methodToBeMocked();
}
}
当我试图在stubbed childClass中模拟methodToBeMocked时。
$this->mockChildClass = $this->getMockBuilder('\childClass')
->setMockClassName('Mock_Child_Class')
->setConstructorArgs(
[
// arguments
]
)
->setMethods(['methodToBeMocked'])
->getMock();
$this->mockChildClass
->expects($this->once())
->method('methodToBeMocked')
->will($this->returnValue(null));
不是调用mocked方法和返回值,而是调用实际的baseClass方法。需要调用模拟方法。