预言模拟所有对象的方法

时间:2019-10-06 20:02:42

标签: php unit-testing testing phpunit prophecy

我在单元测试中使用了Prophecy,我想模拟用户对象中的所有方法

class User {


public function age()
{
    return date("Y") - $this->born();
}

public function born()
{
    // Database call or anything else, it's a black box !
}

}

我想测试出生方式改变时的年龄

    public function testProphecyAge()
{
    $user = $this->prophesize(User::class);

    $user->born()
        ->willReturn(2018)
        ->shouldBeCalled();

    $user = $user->reveal();
    $this->assertEquals(1, $user->age());
}

我得到了错误

Prophecy\Exception\Call\UnexpectedCallException: Unexpected method call on Double\User\P1

0 个答案:

没有答案