模拟atLeastOnce具有具体价值,其余不重要

Ale*_*lex 5 php phpunit unit-testing mocking xunit

问题在于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
Run Code Online (Sandbox Code Playgroud)

在当前代码中,测试在第一次调用之后失败,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");
Run Code Online (Sandbox Code Playgroud)

edo*_*ian 4

你可以,但是我能想到的使用 PHPUnits 模拟 API 的最佳实现仍然看起来相当令人毛骨悚然。

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

但对于挑战:

假设这个类:

<?php
class FancyMocking {
    function doThing($value) { }
}
Run Code Online (Sandbox Code Playgroud)

我们有$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));
    }


}
Run Code Online (Sandbox Code Playgroud)

这将产生:

PHPUnit 3.7.9 by Sebastian Bergmann.

.bool(true)
bool(false)


Time: 0 seconds, Memory: 2.75Mb

OK (1 test, 2 assertions)
Run Code Online (Sandbox Code Playgroud)

这意味着 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.
Run Code Online (Sandbox Code Playgroud)

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

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