【问题标题】:PHP Prophecy ->reveal() method - should I put the expectations (like "willReturn", "ShouldBeCalled") before or after it?PHP Prophecy ->reveal() 方法 - 我应该将期望(如“willReturn”、“ShouldBeCalled”)放在它之前还是之后?
【发布时间】:2021-01-14 13:16:28
【问题描述】:

现在使用 PHP Prophecy。我有两个代码示例: 一:

$aProphecy = $this->prophesize(A::class);
$aProphecy->someMethod()->willReturn([]);
//now can be used:
$aProphecy->reveal();

两个

$aProphecy = $this->prophesize(A::class);
$aProphecy->reveal();
$aProphecy->someMethod()->willReturn([]);

我不明白哪个是正确的方法,为什么?

【问题讨论】:

    标签: php testing prophecy


    【解决方案1】:

    此时您的示例顺序无关紧要,因为这两种情况都会被评估。 不过,我更喜欢第一种变体,因为它更清楚 $aProphecy 的预期。

    需要注意的重要一点是,您的代码没有检查任何内容,因为您需要将 $aProphecy 注入另一个对象并在那里显示它:

    $aProphecy = $this->prophesize(A::class);
    $aProphecy->someMethod()->willReturn([]);
    $bObject = new B($aProphecy->reveal());
    $bObject->methodWhichCallSomeMethodInside();
    // check if someMethod was called exactly once
    $aProchecy->someMethod()->shouldBeCalled(); 
    

    【讨论】:

      猜你喜欢
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      相关资源
      最近更新 更多