5

问题在 中PHP,但适用于使用该xUnit框架的任何语言。

我想要一个模拟,期望对方法进行 140 次调用jump
我需要验证,至少有一次以 500 作为参数的调用。
我不在乎是否所有呼叫都是 500,但我至少需要一个以 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");
4

1 回答 1

4

您可以使用 PHPUnits 模拟 API 的最佳实现,我可以想出的,仍然看起来很令人毛骨悚然。

解决此问题的另一种方法是更具可读性的方法是创建您自己的子类Trampoline并在那里实现它。

但对于挑战:

假设这个类:

<?php
class FancyMocking {
    function doThing($value) { }
}

并且我们有$x电话,其中一个必须有$value > 200


<?php

class FancyMockingTest extends PHPUnit_Framework_TestCase {

    public function testAtLeastOfMy200CallsShouldHaveAValueGreaterThan500() {
      $maxInvocations = 200;

      $mock = $this->getMock('FancyMocking');
      $mock->expects($this->exactly($maxInvocations))
        ->method('doThing')
        ->with($this->callback(function ($value) use ($maxInvocations) { 
            static $invocationCount = 0;
            static $maxValue = 0;

            $maxValue = max($value, $maxValue);
            /* The assertion function will be called twice by PHPUnit due to implementation details, so the *2 is a hack for now */
            if (++$invocationCount == $maxInvocations * 2) { 
                $this->assertGreaterThan(200, $maxValue, 'in 500 tries the max value didn\'t to over 200');
            } 
            return true;
        }))
        ->will($this->returnCallback(function ($value) { 
            return $value >= 200;
        }));
     for($i = $maxInvocations - 2; $i; --$i) { 
          $mock->doThing(50);
     } 
     var_dump($mock->doThing(250));
     var_dump($mock->doThing(50));
    }


}

这将产生:

PHPUnit 3.7.9 by Sebastian Bergmann.

.bool(true)
bool(false)


Time: 0 seconds, Memory: 2.75Mb

OK (1 test, 2 assertions)

这意味着带有 250 的调用返回 true 并且整个测试用例都有效。

如果失败:

为了使它失败,我们更改var_dump($mock->doThing(250));var_dump($mock->doThing(70));并再次运行它:

PHPUnit 3.7.9 by Sebastian Bergmann.

Fbool(false)


Time: 0 seconds, Memory: 2.75Mb

There was 1 failure:

1) FancyMockingTest::testAtLeastOfMy200CallsShouldHaveAValueGreaterThan500
Expectation failed for method name is equal to <string:doThing> when invoked 200 time(s)
in 500 tries the max value didn't to over 200
Failed asserting that 70 is greater than 200.

.../FancyMockingTest.php:29

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

不使用 PHPUnits 模拟 API 的解决方案类似于

class FancyMockingFakeImplementation extends FancyMocking,使用它而不是模拟并在那里编写自定义逻辑。

于 2012-11-25T13:27:49.233 回答