php模拟->;未调用expects()时不报告错误

php模拟->;未调用expects()时不报告错误,php,unit-testing,mocking,phpunit,Php,Unit Testing,Mocking,Phpunit,我有以下模拟对象: $permutator = $this->getMockBuilder('PermutationClass', array('get_permutation'))->disableOriginalConstructor()->getMock(); $permutator->expects($this->at(0)) ->method('get_permutation')

我有以下模拟对象:

$permutator = $this->getMockBuilder('PermutationClass', 
array('get_permutation'))->disableOriginalConstructor()->getMock();

$permutator->expects($this->at(0))
                  ->method('get_permutation')
                  ->will($this->returnCallback(function($praram1) {
                        return true;
                  }));
$permutator->expects($this->at(1))
                  ->method('get_permutation')
                  ->will($this->returnCallback(function($praram1) {
                        return true;
                  }));
然而,我所经历的是,如果出于某种原因,在“1”处的调用从未执行过,那么就没有关于从未满足预期的错误报告

如果我添加以下代码:就在预期之前:

$permutator->expects($this->exactly(2))->method('get_permutation');
然后,如果从未调用给定的期望,则会报告错误。然而,这里发生的事情是,出于某种原因,这使得mock对象的返回值为NULL,因为我没有设置它。如果我这样设置:

$permutator->expects($this->exactly(2))->method('get_permutation')->will($this->returnValue("THIS SHOULD NEVER BE RETURNED"));
然后,这将成为该函数的所有预期方法调用的返回值。因此,在(0)和(1)处执行(我设置了一些打印语句),但返回值被以下内容覆盖:

$permutator->expects($this->exactly(2))->method('get_permutation');
我通过以下方式获得了预期的行为:

$permutator->expects($this->exactly(2))
           ->method('get_permutation')
           ->will( $this->onConsecutiveCalls(
                       $this->returnCallback(function($praram1) {
                           return true;
                       }),
                       $this->returnCallback(function($praram1) {
                           return false;
                       })
                   )
           );

我的意思是,为什么mock对象不会抱怨说,在我明确设置了期望值的情况下,say$this->at(1)从未被调用?

这不是
所期望的
所做的-它所做的只是告诉mock在调用该函数时返回什么。这不是一种断言

如果您想断言调用了一个方法,我将研究

或者,您可以使用
returnCallback
保存调用的参数,然后将其与您知道应该调用的参数进行比较,例如:

$params = [];
$permutator->expects($this->any())
->method('get_permutation')
->will($this->returnCallback(
    function($param) use (&$params){
        $params[] = $param);
    }
));

doTheThing();

$this->assertEquals(
  array(1,2),
  $params
);

我明白你的意思。虽然,“->expected($this->justice(2))->…”正是这样做的。然而,我想我对“->expects(在(…)”的期望并不完全正确。我成功地实现了我在ConceutiveCalls上尝试执行的操作。
expects()
设置了一个期望,并在测试结束时进行验证。期望是断言。您确定没有调用该方法吗?我用$this->at()做了一个简单的例子,如果只调用一次该方法,测试就会失败。我认为在你的代码中还有其他的东西。