Running Laravel 7 with the job queue using the 'database' driver.
There is a call from the frontend to an API that dispatches a job and immediately returns a response with the status (queued). This works fine when called from the frontend Javascript.
Trying to test this same endpoint with PHPUnit, with a test that calls the same API endpoint, I see that the job appears to be dispatched in sync mode, i.e. the HTTP response does not arrive until the job has completed, because the ->dispatch() method does not return until then.
Both tests are using exactly the same dev environment - one runs async, the other sync.
Can't see anything in the docs about this. How to make the job get queued asynchronously when running with PHPUnit, so we can test the intended behavior?
Queue::fake()
andEvent::fake()
. This way you can test of a job way dispatched, without actually running the job. Testing does not necessarily require you to run complete flows. Rethink your tests, what do you really want to test? – Maarten Veerman