Test Dispatch After Response
Mostafa Akram • August 7, 2021
snippets laravelIf your code calls dipatch(SomeAwesomeJob::class)->afterResponse()
instead of just dispatch(SomeAwesomeJob::class)
to delay dispatching the job until after the HTTP response is sent to the user's browser here is how to test it in PHPUnit
use Illuminate\Support\Facades\Bus;
use App\Jobs\SendNotification;
/** @test */
public function job_dispatched_after_response()
{
Bus::fake();
// Call the code that fire the dispatchAfterResponse method
Bus::assertDispatchedAfterResponse(SendNotification::class);
}
- Use
Bus::fake()
instead ofQueue::fake()
- Call
Bus::assertDispatchedAfterResponse()
with the job that should have been dispatched
References
- Laravel Docs for dispatchAfterResponse
- Pull request on laravel/framework by Fabian Bettag
Thanks for reading i hope you liked it
Drop me a message at @mostafakram 👋 and let me know what you think
If you found any mistake or issue please report it on Github