首页 > 解决方案 > 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');
    }
}

在我的集成测试中,我想确保:


到目前为止,我设法验证是否使用Http::assertSentCount(int $count).
解决方案可以获取我正在测试的控制器调用的 URL 序列,但似乎没有办法。

标签: phplaraveltestingphpunitintegration-testing

解决方案


您可以使用 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();

推荐阅读