Laravel integration test: how to assert an URL has been called but another URL no(Laravel 集成测试:如何断言一个 URL 已被调用,但另一个 URL 没有)
问题描述
我想测试一个向某个 URL 发出请求的控制器(例如:http://example.com/api/say-hello
),但它没有向另一个网址(例如:http://example.com/api/say-bye-bye
).
I would like to test a controller that makes a request to an certain URL (EG: http://example.com/api/say-hello
) but it does not make a request to another URL (EG: http://example.com/api/say-bye-bye
).
我要测试的控制器功能如下:
The controller function I want to test looks like this:
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');
}
}
在我的集成测试中,我想确保:
In my integration tests, I want to make sure that:
- 如果参数
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
不得调用.
- If the parameter
say_bye_bye
isTRUE
then both of the API endpoints/api/say-hello
and/api/say-bye-bye
must be called. - If the parameter
say_bye_bye
isFALSE
then just the API/api/say-hello
has to be called and the API endpoint/api/say-bye-bye
must NOT be called.
到目前为止,我设法验证是否使用 Http::assertSentCount(int $count)
进行了一两次调用.
解决方案可以是获取我正在测试的控制器调用的 URL 序列,但似乎没有办法.
So far I managed to verify if one or two calls have been made using Http::assertSentCount(int $count)
.
The solution can be getting the sequence of URLs called by the controller I'm testing, but it seems like there is no way.
推荐答案
可以使用Http::spy()
You can use 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();
这篇关于Laravel 集成测试:如何断言一个 URL 已被调用,但另一个 URL 没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 集成测试:如何断言一个 URL 已被调用,但另一个 URL 没有


基础教程推荐
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- php中的PDF导出 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- php中的foreach复选框POST 2021-01-01
- Web 服务器如何处理请求? 2021-01-01