PHPUnit: how do I mock multiple method calls with multiple arguments?

后端 未结 4 2205

I am writing a unit test for a method using PHPUnit. The method I am testing makes a call to the same method on the same object 3 times but with different sets of arguments.

4条回答
  •  清歌不尽
    2020-12-12 22:20

    In case someone finds this without looking at the correspondent section in the phpunit documentation, you can use the withConsecutive method

    $mock->expects($this->exactly(3))
         ->method('MyMockedMethod')
         ->withConsecutive(
             [$arg1, $arg2, $arg3....$argNb],
             [arg1b, $arg2b, $arg3b....$argNb],
             [$arg1c, $arg2c, $arg3c....$argNc]
             ...
         );
    

    The only downside of this being that the code MUST call the MyMockedMethod in the order of arguments supplied. I have not yet found a way around this.

提交回复
热议问题