gpt4 book ai didi

php - makePartial() 返回 Mockery\Exception\BadMethodCallException : Method does not exist on this mock object

转载 作者:行者123 更新时间:2023-12-01 18:56:35 25 4
gpt4 key购买 nike

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

运行测试时出现此错误:

Mockery\Exception\BadMethodCallException:此模拟对象上不存在方法 App\Models\Category::getDynamicFieldsForDocument()

这是我的代码:

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”

关于php - makePartial() 返回 Mockery\Exception\BadMethodCallException : Method does not exist on this mock object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55301305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com