69

这就是我所拥有的:

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

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

如何测试第二个参数?

4

1 回答 1

110

我相信这样做的方法是:

$observer->expects($this->once())
     ->method('method')
     ->with($this->equalTo($arg1),$this->equalTo($arg2));

或者

$observer->expects($this->once())
     ->method('method')
     ->with($arg1, $arg2);

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

$observer->expects($this->once())
     ->method('method')
     ->with($this->equalTo($arg1),$this->stringContains('some_string'));

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

$observer->expects($this->once())
     ->method('method')
     ->with($this->logicalAnd($this->stringContains('a'), $this->stringContains('b')));
于 2008-12-08T21:16:05.213 回答