PHP Prophecy Stub方法未调用

时间:2017-04-26 13:40:59

标签: php phpunit prophecy

我无法通过这个明显的测试。 Foo在其构造函数中获取一个Bar,当调用Foo :: m()时,调用Bar :: bar()。

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未能以某种方式将调用注册到Bar :: bar()......

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

1 个答案:

答案 0 :(得分:1)

您的$bar变量包含ObjectProphecy的实例,该实例与Bar类无关。致电$bar->reveal()以获得测试双倍,这是Bar的扩展名:

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