Yii phpunit getMockBuilder doesn't work with --filter

In a Yii project, I am using phpunit with getMockbuilder. When I run unit tests for the whole file, they all pass. However, when I do phpunit --filter testMyFunction

, I get the following error. "Calling undefined method Mock_Account_3a811374 :: __ construct () ..."

After a little checking, I see that if the -filter ends up including a test that doesn't use a mock in addition to the one that does, then it works fine.

Anyone have any ideas on how to fix it?

Here are some of my code (simplified) ...

use components\Account;

class UtilsTest extends CDbTestCase
{
    ...
    public function testMyFunction()
    {
        $accountStub = $this->getMockBuilder('Account')
            ->disableOriginalConstructor()
            ->setMethods(array('methodToStub'))
            ->getMock();

        $accountStub->expects($this->any())
            ->method('methodToStub')
            ->will($this->returnValue(false));

        $accountStub->__construct();
    ...
    }
}

      

+3


source to share


1 answer


I'm confused about what is trying to do with $accountStub->__construct()

when you indicated that you don't want to call the original constructor with ->disableOriginalConstructor()

?

You can call the constructor yourself and pass parameters using setConstructorArgs(array $args)

. So it will look something like this:

$accountStub = $this->getMockBuilder('Account')
        ->setConstructorArgs($args)
        ->getMock();

      

However, it would be pointless to call it at the same time as disableOriginalConstructor()

. I think you probably want to do this or that.



I believe that the error message you receive is simply PHP telling you that you are trying to do something that doesn't make sense. You are trying to call a constructor on an object that has already been created. What's more, you even specifically told the mock to skip calling the constructor method. PHP just tells you that this method doesn't exist.

I think you probably just need to remove the following line and check again:

$accountStub->__construct();

      

0


source







All Articles