I have an event listener that listens for the JobProcessing
event. I want to test this event listener, to do so I need to simulate/mock the JobProcessing
queue event. TestJob
is a regular Laravel job class.
<code>// attempt 1, results in:
// Expected parameter of type 'IlluminateContractsQueueJob', '<namespace>TestJob'
public function test_updates_job_status_to_processing_when_job_starts_processing(): void
{
$event = new JobProcessing(
'default',
new TestJob(),
);
$eventListener = $this->app->make(UpdateJobStatus::class);
$eventListener->handle($event);
// assertions
}
</code>
<code>// attempt 1, results in:
// Expected parameter of type 'IlluminateContractsQueueJob', '<namespace>TestJob'
public function test_updates_job_status_to_processing_when_job_starts_processing(): void
{
$event = new JobProcessing(
'default',
new TestJob(),
);
$eventListener = $this->app->make(UpdateJobStatus::class);
$eventListener->handle($event);
// assertions
}
</code>
// attempt 1, results in:
// Expected parameter of type 'IlluminateContractsQueueJob', '<namespace>TestJob'
public function test_updates_job_status_to_processing_when_job_starts_processing(): void
{
$event = new JobProcessing(
'default',
new TestJob(),
);
$eventListener = $this->app->make(UpdateJobStatus::class);
$eventListener->handle($event);
// assertions
}
I then tried the following, (Queue::pop
returns an actual job instance):
<code>// attempt 2, results in:
// Call to a member function uuid() on null (so Queue::pop returned null)
public function test_updates_job_status_to_processing_when_job_starts_processing(): void
{
Queue::push(new TestJob);
$event = new JobProcessing(
'default',
Queue::pop(),
);
$eventListener = $this->app->make(UpdateJobStatus::class);
$eventListener->handle($event);
// assertions
}
</code>
<code>// attempt 2, results in:
// Call to a member function uuid() on null (so Queue::pop returned null)
public function test_updates_job_status_to_processing_when_job_starts_processing(): void
{
Queue::push(new TestJob);
$event = new JobProcessing(
'default',
Queue::pop(),
);
$eventListener = $this->app->make(UpdateJobStatus::class);
$eventListener->handle($event);
// assertions
}
</code>
// attempt 2, results in:
// Call to a member function uuid() on null (so Queue::pop returned null)
public function test_updates_job_status_to_processing_when_job_starts_processing(): void
{
Queue::push(new TestJob);
$event = new JobProcessing(
'default',
Queue::pop(),
);
$eventListener = $this->app->make(UpdateJobStatus::class);
$eventListener->handle($event);
// assertions
}
How can I properly test my event listener’s interaction with the Laravel queue events?
New contributor
user25728292 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.