makePartial() returns Mockery\Exception\BadMethodCallException : 此模拟对象上不存在方法

makePartial() returns Mockery\Exception\BadMethodCallException : Method does not exist on this mock object

我正在尝试测试我的类别 class。我正在使用 Mockery::mock() 方法,带有 'overload:' 前缀和 makePartial() 方法。

当运行测试时我有这个错误:

Mockery\Exception\BadMethodCallException : Method App\Models\Category::getDynamicFieldsForDocument() does not exist on this mock object

这是我的代码:

namespace App\Models;
class Category extends DictionaryBase{
    //some methods
    public function getDynamicFieldsForDocument()
    {
        $data = [];
        $parents = [];
        $allParents = $this->getParents($this->id, $parents);
        foreach ($allParents as $parentId) {

            $parent = Category::find($parentId);
            $fields = $parent->dynamicFields();
            foreach ($fields as $field) {
                $data[$field['name']] = $field;
            }
        }

        return $data;
    }
}

测试用例:

namespace Tests\Unit;

use App\Models\Category;
use Tests\TestCase;
class CategoryModelTest extends TestCase{
    //some methods
    /**
     * @runInSeparateProcess
     * @preserveGlobalState disabled
     */
    public function testGetDynamicFieldsForDocument()
    {
        $mockCategory = \Mockery::mock('overload:'.Category::class)->makePartial();
        $preparedDynamicFields = $this->prepareDynamicFields();
        $preparedCategories = $this->prepareCategories();
        $mockCategory->shouldReceive('find')->andReturn($preparedCategories[0], $preparedCategories[1], $preparedCategories[2]);
        $mockCategory->shouldReceive('getParents')->andReturn(['1a2b', '3c4d', '5e6f']);
        $mockCategory->shouldReceive('dynamicFields')->andReturn(null, $preparedDynamicFields[0], $preparedDynamicFields[1]);

        $response = $mockCategory->getDynamicFieldsForDocument();
        dd($response);
    }
}

我不知道为什么我仍然有错误。我认为当调用 ->makePartial() 方法时,它应该只模拟 ->shouldReceive()

调用的方法

编辑:

现在我正在制作没有 :overload 的模拟实例,并以这种方式模拟 'find' 方法:

    `$mockCategory->shouldReceive('find')->andReturn($preparedCategories[0], $preparedCategories[1], $preparedCategories[2]);`

我的查找方法如下所示:

public static function find($id) {
        return $id ? self::list(config(static::IDENT.'.fields'), (new Filter('and'))->add('id', $id, '')->toArray(),[],1,1)[0] ?? null : null;
    }

这是我的错误:

Error : Wrong parameters for App\Exceptions\ApiException([string $message [, long $code [, Throwable $previous = NULL]]])

因为调用了list方法API所以看起来这个方法是在没有mock的情况下调用的。 我知道我不能模拟静态方法,但早些时候我使用 :overload 时是可能的。现在做什么?

删除 :overload 并将模拟定义为:

$mockCategory = \Mockery::mock(Category::class)->makePartial()

例子

型号:

namespace App\Models;

class Foobar extends BaseModel
{
  public function foonction()
  {   
      Foobar::find();                                                                                                                                                                                          
      return '1';
  }   
}

测试:

命名空间测试;

use Evalua\Heva\Models\Foobar;

class FoobarTest extends TestCase 
{
  public function testFoobar()
  {   
    $fooMock = \Mockery::mock('overload:'.Foobar::class)->makePartial();
    $fooMock->shouldReceive('find')->once();
    $fooMock->foonction();
  }   
}

失败:

Mockery\Exception\BadMethodCallException: Method Evalua\Heva\Models\Foobar::foonction() does not exist on this mock object

没有 :overload 测试通过。

解释应该是based on what's written in the documentation about overload:

Prefixing the valid name of a class (which is NOT currently loaded) with “overload:” will generate an alias mock (as with “alias:”) except that created new instances of that class will import any expectations set on the origin mock ($mock). The origin mock is never verified since it’s used an expectation store for new instances. For this purpose we use the term “instance mock”