有没有办法告诉phpunit模拟对象,如果没有明确定义的期望,就永远不要期望方法调用?
$mock->expects($this->never())
->method($this->anything());
$mock = $this->getMock('Some\Tested\Class', array('testedMethod'));
$mock->expects($this->never())
->method($this->anything());
matchesRegularExpression
:$mock->expects($this->never())
->method($this->matchesRegularExpression('/get.*/'));
expected($this->never())->method('|^goodMethod|')
,意思是它不应该期望调用任何方法,除了叫做goodMethod()
的那个。虽然不完全符合OP的要求,但解决了我的问题。 - rodrigo-silveira$mock->expects($this->any())
->method('foo')
->with(new PHPUnit_Framework_Constraint_Not('bar'));
TestCase::getMockBuilder()
方法。public function getMockBuilder(string $className): MockBuilder
{
return parent::getMockBuilder($className)->disableAutoReturnValueGeneration();
}
$mock->expects($this->never())->method('abc')
。 - Bakyt Abdrasulov