method does not exist on this mock object - Laravel , Mockery

早过忘川 提交于 2019-12-05 18:36:01

Perhaps an aside, but directly relevant to anyone finding this question by its title:

If:

  1. You are getting the error BadMethodCallException: Method Mockery_0_MyClass::myMethod() does not exist on this mock object, and
  2. none of your mocks are picking up any of your subject's methods, and
  3. your classes are being autoloaded, (e.g. using composer)

then before making your mock object, you need to force the loading of that subject, by using this line of code:

spl_autoload_call('MyNamespace\MyClass'); 

Then you can mock it:

$mock = \Mockery::mock('MyNamespace\MyClass');

In my PHPUnit tests, I often put that first line into the setUpBeforeClass() static function, so it only gets called once and is isolated from tests being added/deleted. So the Test class looks like this:

class MyClassTest extends PHPUnit_Framework_TestCase {
    public static function setUpBeforeClass() {
        parent::setUpBeforeClass();
        spl_autoload_call('Jodes\MyClass'); 
    }
    public function testIt(){
        $mock = \Mockery::mock('Jodes\MyClass');
    }
}

I have forgotten about this three times now, each time spending an hour or two wondering what on earth the problem was!

I have found a source of this bug in controller. calling wrong

$v = $mealType->getValidator($input);

instead of right

$v = $this->mealType->getValidator($input);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!