Test Dispatch After Response

Mostafa Akram • August 7, 2021

snippets laravel

If 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); 

}

References

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