In a Yii project, I am using phpunit with getMockbuilder. When I run unit tests on the whole file, they all pass. However, when I do phpunit --filter testMyFunction
, I get the following error. "Call to undefined method Mock_Account_3a811374::__construct() ..."
After doing a little more checking I see that if the --filter ends up including a test that does not use the mock in addition to the one that does, then it works fine.
Anyone have any ideas on how to fix it?
Here's 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();
...
}
}
I'm confused by what are are trying to do with $accountStub->__construct()
when you have specified that you don't want to call the original constructor with ->disableOriginalConstructor()
?
You can call the constructor yourself and pass parameters with setConstructorArgs(array $args)
. So this would look something like this:
$accountStub = $this->getMockBuilder('Account')
->setConstructorArgs($args)
->getMock();
However it wouldn't make sense to call this at the same time as disableOriginalConstructor()
. I think you probably want to do one or the other.
I believe the error message you are getting is just PHP telling you that you are trying to do something that doesn't make sense. You are trying to call the constructor on an object that has already been constructed. What's more, you have even specifically told the mock to skip the call to the constructor method. PHP is just telling you that this method does not exist.
I think you probably just need to remove the following line and test again:
$accountStub->__construct();