如何测试PHPUnit模拟对象中的第二个参数

Joe*_*oel 64 php phpunit

这就是我所拥有的:

$observer = $this->getMock('SomeObserverClass', array('method'));
$observer->expects($this->once())
         ->method('method')
         ->with($this->equalTo($arg1));
Run Code Online (Sandbox Code Playgroud)

但该方法应该采用两个参数.我只测试第一个参数正确传递($ arg1).

如何测试第二个参数?

sil*_*eed 98

我相信这样做的方法是:

$observer->expects($this->once())
     ->method('method')
     ->with($this->equalTo($arg1),$this->equalTo($arg2));
Run Code Online (Sandbox Code Playgroud)

要么

$observer->expects($this->once())
     ->method('method')
     ->with($arg1, $arg2);
Run Code Online (Sandbox Code Playgroud)

如果你需要在第二个arg上执行不同类型的断言,你也可以这样做:

$observer->expects($this->once())
     ->method('method')
     ->with($this->equalTo($arg1),$this->stringContains('some_string'));
Run Code Online (Sandbox Code Playgroud)

如果你需要确保一些参数传递多个断言,请使用logicalAnd()

$observer->expects($this->once())
     ->method('method')
     ->with($this->logicalAnd($this->stringContains('a'), $this->stringContains('b')));
Run Code Online (Sandbox Code Playgroud)

  • @ieure equalT()的第二个参数是$ delta,因此可能不会按照您的想法执行. (7认同)