Php Mockry:BadMethodCallException:MethodMockry\u 0\u Foo::bar()在此模拟对象上不存在

Php Mockry:BadMethodCallException:MethodMockry\u 0\u Foo::bar()在此模拟对象上不存在,php,phpunit,composer-php,mockery,Php,Phpunit,Composer Php,Mockery,我无法让Mocky创建一个简单的假人: <?php require_once '../vendor/autoload.php'; // composer autoload mockery class Foo { private $required; public function __construct($required){ $this->required = $required; } public function bar(){

我无法让Mocky创建一个简单的假人:

<?php
require_once '../vendor/autoload.php'; // composer autoload mockery

class Foo {
    private $required;
    public function __construct($required){
        $this->required = $required;
    }
    public function bar(){
        // do stuff with $this->required
    }
}

class FooTest extends PHPUnit_Framework_TestCase  {
    public function testBar(){
        $mock = \Mockery::mock('\Foo');
        $mock->bar();
    }
}

我做错了什么?

我必须明确说明模拟生成存根的方法:

class FooTest extends PHPUnit_Framework_TestCase  {
    public function testBar(){
        $mock = \Mockery::mock('Foo');
        $mock->shouldReceive('bar');
        $mock->bar();
    }
}
我很好奇是否有办法解决这个问题,或者:

隐式捕获在Foo中定义的所有方法调用,或 隐式捕获所有方法调用,无论它们是否在Foo中定义
如果您想在Foo类和mocks所需对象上进行php单元测试。只需按以下方式操作:

class Foo {
    private $required;
    public function __construct(\Required $required){
        $this->required = $required;
    }
    public function bar(){
        return $this->required->getTextFromBarTable();
    }
}

class FooTest extends PHPUnit_Framework_TestCase  {
    public function testBar(){
        $mock = \Mockery::mock('\Required'); // Dummy, There are no properties or methods.

        /**
        * Stub "getTextFromBarTable" method of \Required class 
        * and fakes response by returning "return this text".
        */
        $mock->shouldReceive('getTextFromBarTable') 
             ->andReturn('return this text');

        // create "Foo" Object by using $mock instead of actual "\Required" Object.
        $foo = new Foo($mock); 
        $response = $foo->bar();

        $this->assertEqual('return this text', $response);
    }
}
不能对要进行单元测试的存根类或模拟类进行单元测试。只需在依赖类上执行,如\Required

我们使用存根或模拟来分离可能影响我们要测试的方法的内部逻辑的外部逻辑。在本例中,我假设\Required类有getTextFromBarTable方法,该方法将连接并从数据库中获取“text”字段。如果我们的数据库没有文本字段,testBar方法将被破坏。为了摆脱外部问题,我在\Required上进行了存根操作,每次都使用getTextFromBarTable方法。它将始终返回此文本

class Foo {
    private $required;
    public function __construct(\Required $required){
        $this->required = $required;
    }
    public function bar(){
        return $this->required->getTextFromBarTable();
    }
}

class FooTest extends PHPUnit_Framework_TestCase  {
    public function testBar(){
        $mock = \Mockery::mock('\Required'); // Dummy, There are no properties or methods.

        /**
        * Stub "getTextFromBarTable" method of \Required class 
        * and fakes response by returning "return this text".
        */
        $mock->shouldReceive('getTextFromBarTable') 
             ->andReturn('return this text');

        // create "Foo" Object by using $mock instead of actual "\Required" Object.
        $foo = new Foo($mock); 
        $response = $foo->bar();

        $this->assertEqual('return this text', $response);
    }
}