php - 依赖项中的嘲弄和方法链接
问题描述
我对测试很陌生,尤其是对嘲弄。
在 Laravel 项目中,我有一个包装外部 api 库的类。像这样的东西:
class ApiWrapper {
private $api;
public function __construct(ExternalApiLibrary $api)
{
$this->api = $api;
}
public function method()
{
$this->api->method1('Foo')
->method2('Bar')
->method3('Not');
}
}
在我的测试类中,我ExternalApiLibrary
使用 mockery 进行模拟,我想测试这 3 个方法是否使用它们的参数调用。
public function test_that_works()
{
$this->mock(ExternalApiLibrary::class, function (MockInterface $mock) {
$mock
->shouldReceive('method1')
->with('Foo');
$mock
->shouldReceive('method2')
->with('Bar');
$mock
->shouldReceive('method3')
->with('Not');
});
$apiWrapper = $this->app->make(ApiWrapper::class);
$apiWrapper->method();
}
但我明白了
Method Mockery_0_ExternalApiLibrary::method2() does not exist on this mock object
。
我理解原因,但我如何测试这些方法是否在该外部库上被调用?
解决方案
推荐阅读
- powershell - 使用 powershell 在 SSRS 中更改凭据检索
- node.js - 创建一个bat文件来运行节点命令
- c++ - 如何循环程序在 C++ 中请求新的输入集?
- ignite - 如何在 ignite-sql-api 中设置 rebalanceMode?
- c# - Post方法不在数据库表Asp.net mvc web api中添加值
- vba - 显示我刚刚发送的电子邮件
- android - 开始实际方法调用 Android UI 测试时的 Mockito
- javascript - 可以使用 Babel 转译成 TypeScript 吗?
- modelica - 访问其他模型中的参数 - OpenModelica
- java - 如何在springboot中向请求正文添加额外属性并插入数据库