mardi 5 novembre 2019

phpunit mock does not count the function call

I have a unit test where I create a mock object:

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

I make this a return value of another mock object

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

I check if it was called exactly one time:

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

if I put a var_dump just before the call in the actual unit I see that I pass the code there, and that the stub is the resetTokenManagerStub, but still the test result is:

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.

Why doesn't it count the call? Is it because I used it as a return value of another mock? The object I get when I call getManagerForClass is the same as the one in the unittest. I did a ar_dump on it and both look the same:


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

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

What am I doing wrong here ?

Aucun commentaire:

Enregistrer un commentaire