Skip to content
Advertisement

PhpUnit How to test double a method using both other class and self class methods?

So I want to test double my function doEverything like so :

JavaScript

And the corresponding test i’m trying to use look like this :

JavaScript

This above return me that doSomethingSecondClass is never called in doEverything method from MyClass. Thought, I can use $this->myClass->expects without troubles.

And the following test code :

JavaScript

The above code tell me that myClass cannot be used with expects function from phpUnit, but the doSomethingSecondClass is called as expected.

So, in your opinion, how should I proceed to test this method ?

Thanks !

Advertisement

Answer

The secondClass is never used because you are using a mock for the myClass. In the mock you never call the secondClass. The right way to test this would be to not use a mock for myClass, but instead instantiate a real instance of it.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement