- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试测试我的类别类(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/
我们有大量使用 JMock 2.5.1 编写的单元测试,其中大多数(全部?)使用本地 Mockery 对象,例如: @RunWith(JMock.class) public class SomeTes
我编写了一个从 aws s3-bucket 获取单个文件的导入函数。 该函数本身是 aws.s3::s3read_using() 的包装器,它将读取函数作为其第一个参数。 为什么要环绕 aws.s3:
我刚开始使用 Mockery并与术语 alias 和 overload 混淆。谁能给我解释一下什么时候使用哪个? 最佳答案 Overload 用于创建“实例模拟”。当创建一个类的新实例时,这将“拦截”
我使用 Laravel (4.1) 框架并阅读了“Laravel-testing-decoded”,这是 Jeffrey Wey 的电子书。 我想测试我的模态用户和我的方法 setPasswordAt
我从一周开始学习 phpunit。我不知道如何只模拟测试类中的一种方法。 (这只是示例,所以我没有写命名空间)。也许你可以帮助我 class SomeService { public func
我希望能够使用 Mockery 在 java 中模拟 File 对象。我似乎无法在 Java 中为文件创建接口(interface)。这可能吗? 编辑: 我需要测试 Indexer 类中的 index
我正在使用 phpunit 和 mockery 在 laravel 中学习单元测试。我目前正在尝试测试 UsersController::store()。 我正在模拟用户模型并使用它来测试索引方法,这
我需要测试,代码创建了一个具有特定参数的类的新实例: $bar = new ProgressBar($output, $size); 我试图创建一个别名 mock 并为 __construct 方法设
我正在尝试在 Controller 中正确模拟对 Eloquent 模型的链式调用。在我的 Controller 中,我使用依赖注入(inject)来访问模型,以便模拟它应该很容易,但是我不确定如何测
我正在尝试测试一个简单的类。我正在学习本教程 ( http://code.tutsplus.com/tutorials/testing-laravel-controllers--net-31456 )
我想使用Mokcery mock一个已经在另一个方法中使用过的静态方法,如下: Class SomeClass { public static function methodA() {
TLDR;使用相同参数调用 mock shouldReceive('method)` 时,如何第一次获取 value1 和第二次获取 value2? 假设我有一个 $order 对象,该对象使用 ge
我想使用 Mockery 并改变它: $mockFoo = $this->getMockBuilder('Foo') ->disableOriginalConstructor()
如果使用 JMock 编写带有模拟的 Java 单元测试,我们应该使用 Mockery context = new Mockery() 或者 Mockery context = new JUnit4M
我正在尝试为我的网络应用程序编写一些单元测试。我使用 Laravel 5.3 和 Mockery。 这是简化的代码: class UserService { protected $userRe
我能以某种方式更新特定 shouldReceive 的 shouldReturn 值吗? 例如,我有几个使用一个返回值的测试,只有一个使用其他返回值。 所以我把它放在了 setUp 中: $this-
我目前正在为 Laravel 4 中的一个包编写测试。 我在模拟 Illuminate\Database\Query\Builder 它几乎一直工作,除非 where 方法使用回调,我无法检查内部方法
这可能是我对模拟的误解。我真的很感激关于为什么模拟一个不存在或存在的类是一件好事的解释? 示例场景:假设我们有一个类依赖于第二个类的输出,我们更改了输出格式或其他格式。这不会让我们的测试保持成功,尽管
我正在尝试测试我的一个服务类,但无法通过 PHPUnit 测试。 测试失败的相关部分: File::shouldReceive('put')->with('app/storage/logs/larav
如何在流明框架中使用模拟? 我使用流明框架。 lumen的文档很简单。我不知道如何使用 mockery 或 facades 来模拟模型。我尝试了一些方法,但没有人奏效。我想在 updatePasswo
我是一名优秀的程序员,十分优秀!