phpunit模拟不计算函数调用

我有一个创建模拟对象的单元测试:

 $resetTokenmanagerStub = $this->getMockBuilder(ObjectManager::class)->getMock();

我将此作为另一个模拟对象的返回值

 $managerRegistryStub->method('getManagerForClass')->willReturn($resetTokenmanagerStub);

我检查一次是否被正确调用:

 $resetTokenmanagerStub->expects($this->once())->method('persist');

如果我在实际单元中的调用之前放置了一个var_dump,我看到我在那里传递了代码,并且存根为resetTokenmanagerStub,但测试结果仍然是:

There was 1 failure:

1) App\Tests\Service\UserServiceTest::testGeneratePasswordResetToken with data set #0 (Mock_User_a375caf8 Object (...),'generatedToken')
Expectation failed for method name is equal to 'persist' when invoked 1 time(s).
Method was expected to be called 1 times,actually called 0 times.

为什么不计算通话次数?是否因为我将其用作另一个模拟的返回值?调用getManagerForClass()时得到的对象与单元测试中的对象相同。我在上面做了ar_dump,看起来都一样:


object(Mock_ObjectManager_142d72a2)#26 (4) {
...

object(Mock_ObjectManager_142d72a2)#26 (4) {
...

我在这里做什么错了?

lxwtsqr 回答:phpunit模拟不计算函数调用

再一次我很愚蠢。对不起,这篇文章。这就像橡皮鸭法。我挣扎了一个小时,当我将其提交到论坛时,我发现自己做错了...

在实际函数调用之后,正在调用$resetTokenManagerStub->expects($this->once())->method('persist');行...

本文链接:https://www.f2er.com/3160319.html

大家都在问