php - PHP Prophecy ->reveal() 方法 - 我应该将期望(如“willReturn”、“ShouldBeCalled”)放在它之前还是之后?
问题描述
现在使用 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([]);
我不明白哪个是正确的方法,为什么?
解决方案
此时您的示例顺序无关紧要,因为这两种情况都将被评估。但是,我更喜欢第一个变体,因为它更清楚 $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();
推荐阅读
- javascript - Chrome Devtools 以什么方式异步记录对象?(特别是在 React Native 中)
- r - 替换为最右边行字符串右侧的 NA
- python - Pusher:Swift (iOS) 发布者/服务器到 Raspberry Python 订阅者/客户端
- c# - 复选框的功能或循环
- azure-ad-b2c - EnforceChangePasswordPolicy 和 ForceChangePasswordNextLogin 有什么区别?
- python - 在频谱图上绘制基于时间的线图
- javascript - 在 Google 地图中打开/关闭标记
- javascript - 尝试发送嵌入时我不断收到错误
- python-3.x - 当我运行一个 tweepy python 脚本时,关联的 Twitter 帐户上没有任何反应
- javascript - 如何使用 cytoscape.js 在单个动画中沿特定方向移动所有节点?