0

我无法通过这个明显的测试。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.
4

1 回答 1

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();
}
于 2017-04-26T14:18:03.600 回答