質問は にありますが、フレームワークPHP
を使用するすべての言語に適用されますxUnit
。
method への 140 回の呼び出しを期待するモックが必要ですjump
。パラメータとして 500 を使用した呼び出しが少なくとも 1 回
ある
ことを確認する必要があります。
すべての呼び出しが 500 であるかどうかは気にしませんが、少なくとも 1 つは 500 で呼び出される必要があります。
$mock = $this->getMock('Trampoline', ['jump']);
$mock->expects($this->atLeastOnce())
->method('jump')
->with($this->equalTo(500))
->will($this->returnValue(true));
$sportsman->setTramploine($mock);
$sportsman->jumpToRandomHeights($times = 140); // this calls Trampoline->jump
// I need to verify the sportsman had jumped
// to the height of 500 at least once out of the 140 jumps he is performing
現在のコードでは、最初の呼び出しjump
の値が と異なるため、テストは の最初の呼び出しの後に失敗します。これは、メソッドを呼び出す必要があることのみを示しており、他の呼び出しの中で特定の値で呼び出す必要があることを示しているわけではありません 500
。atLestOnce
解決
不足している情報は、 内でコールバックを使用していたことwith
です。以下のedorianの回答のおかげで、これがうまくいきました:
$testPassed = false;
$checkMinHeight = function ($arg) use(&$testPassed)
{
if($arg === 500)
$testPassed = true;
// return true for the mock object to consider the input valid
return true;
}
$mock = $this->getMock('Trampoline', ['jump'])
->expects($this->atLeastOnce())
->method('jump')
->with($checkMinHeight)
->will($this->returnValue(true));
$sportsman->setTramploine($mock);
$sportsman->jumpToRandomHeights($times = 1000); // this calls Trampoline->jump
// I need to verify the sportsman had jumped
// to the height of 500 at least once out of the 1000 jumps he is performing
$this->assertTrue($testPassed, "Sportsman was expected to
jump 500m at least once");