php - Laravel 集成测试:如何断言一个 URL 已被调用,但另一个 URL 没有
问题描述
我想测试一个向某个 URL (EG: http://example.com/api/say-hello
) 发出请求但不向另一个 URL (EG: http://example.com/api/say-bye-bye
) 发出请求的控制器。
我要测试的控制器功能如下所示:
public function callApis(Request $request)
{
$data = $this->validate($request, [
'say_bye_bye' => 'nullable|boolean',
]);
Http::get('http://example.com/api/say-hello');
if ($data['say_bye_bye']) {
Http::get('http://example.com/api/say-bye-bye');
}
}
在我的集成测试中,我想确保:
- 如果参数
say_bye_bye
是TRUE
两个 API 端点/api/say-hello
并且/api/say-bye-bye
必须被调用。 - 如果参数
say_bye_bye
是,FALSE
则只需调用 API/api/say-hello
,不得调用 API 端点/api/say-bye-bye
。
到目前为止,我设法验证是否使用Http::assertSentCount(int $count)
.
解决方案可以获取我正在测试的控制器调用的 URL 序列,但似乎没有办法。
解决方案
您可以使用 Http::spy()
Http::spy()
->expects('get')
->with('http://example.com/api/say-hello')
->once();
Http::spy()
->expects('get')
->with('http://example.com/api/say-bye-bye')
->never();
推荐阅读
- r - 如何更改 R 中 geom_dotplot 中点的形状?
- angular - 使用angular和chartjs在一页上显示多个图表
- android - 找不到变量:文档
- firebase - 在我的 firebase 控制台中找不到用于我的颤振项目注册身份验证的 Api-key
- reactjs - 我刚刚从 github 将一个 react 应用程序克隆到我的本地计算机,但是当我在其上运行 npm start 时,出现此错误:
- amazon-web-services - aws ec2 自动缩放组的“平均网络输入”的单位是什么
- java - Java 整数溢出以及为什么是 Integer.MAX_VALUE / 10?
- swift - 视图中不可变的某些视图主体变量/不透明返回
- reactjs - 如何在 React/React Native 中设置多个过滤器
- javascript - react native 中的函数组件