I've developed a Laravel console command that makes a HTTP request to an external API.
In the feature test, I want to check that the correct URL has been called.
The problem is that in the feature test I'm not able to catch the request made from the command.
Below an example of the code of the artisan command:
// ...
use Illuminate\Support\Facades\Http;
class SyncGamificationAccount extends Command
{
// ...
public function handle()
{
Http::get('http://example.com/api/some-endpoint');
$this->info("Done :)");
return 0;
}
}
Please find below the code of the feature test:
use Illuminate\Support\Facades\Http;
// ...
/** @test */
public function the_command_console_should_make_the_request()
{
Http::fake();
$resp = $this->artisan('users:update-from-api');
$expectedUrl = 'http://example.com/api/some-endpoint';
Http::assertSent(
function ($request) use ($expectedUrl) {
dd('not triggered at all');
return $request->url() == $expectedUrl;
}
);
}
When I run the tests I get the following output:
1) Tests\Feature\Console\UsersUpdateFromApiTest::the_command_console_should_make_the_request
An expected request was not recorded.
Failed asserting that false is true.
/srv/http/laravel-prj/vendor/laravel/framework/src/Illuminate/Http/Client/Factory.php:223
/srv/http/laravel-prj/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261
/srv/http/laravel-prj/tests/Feature/Console/UsersUpdateFromApiTest.php:61
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
It seems like the block of code above within Http::assertSent(...) is not triggered at all since no API request has been intercepted; in fact when running the test, the die and dump dd('not triggered at all'); is not shown.
I used lots of times Http::fake(); and Http::assertSent but it seems like for the console command it doesn't work as usual.
Note that when running my command php artisan users:update-from-api it makes correctly the request.
Thank you a lot for your help!