PHPUnitモックオブジェクトの2番目のパラメーターをテストする方法
質問
これは私が持っているものです:
$observer = $this->getMock('SomeObserverClass', array('method'));
$observer->expects($this->once())
->method('method')
->with($this->equalTo($arg1));
ただし、メソッドは2つのパラメーターを使用する必要があります。最初のパラメーターが正しく渡されることをテストしています($ arg1として)。
2番目のパラメーターのテスト方法
解決
これを行う方法は次のとおりです:
$observer->expects($this->once())
->method('method')
->with($this->equalTo($arg1),$this->equalTo($arg2));
または
$observer->expects($this->once())
->method('method')
->with($arg1, $arg2);
2番目の引数で異なるタイプのアサーションを実行する必要がある場合は、それも実行できます。
$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')));
所属していません StackOverflow