dounieliang4712 2017-04-26 13:40
浏览 30
已采纳

PHP Prophecy Stub方法未调用

I can't get this obvious test to pass. Foo gets a Bar in its constructor and when calling Foo::m(), Bar::bar() gets called.

use PHPUnit\Framework\TestCase;

class Bar {
    public function bar() {
        echo "BAR";
    }
}

class Foo {
    protected $bar;
    public function __construct($bar) {
        $this->bar= $bar;
    }
    public function m() {
        $this->bar->bar();
    }
}

class FooTest extends TestCase {

    public function testM() {
        $bar = $this->prophesize(Bar::class);
        $bar->bar()->shouldBeCalled();
        $foo = new Foo($bar);
        $foo->m();
    }
}

Prophecy fails to register the call to Bar::bar() somehow...

Some predictions failed:
  Double\Bar\P1:
    No calls have been made that match:
      Double\Bar\P1->bar()
    but expected at least one.
  • 写回答

1条回答 默认 最新

  • dongzhimin2231 2017-04-26 14:18
    关注

    Your $bar variable contains an instance of ObjectProphecy, which is unrelated to the Bar class. Call $bar->reveal() to get a test double which is an extension of Bar:

    public function testM()
    {
        $bar = $this->prophesize(Bar::class);
        $bar->bar()->shouldBeCalled();
        $foo = new Foo($bar->reveal());
        $foo->m();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭