Summer Sale! All accounts are 50% off this week.

chaudigv's avatar

How to assert after a job queue is pushed?

I have a job under queue that does some calculations and then saves it in DB.

Now I have the job test code

Queue::fake();

$user = User::first();

Queue::assertPushed(TheJob::class);

// confirm user's new data
$user->fresh() // still the job queue's calculations are not reflected.

My question is, how can I confirm that the queue's calculated data now exists in DB?

0 likes
4 replies
martinbean's avatar

@chaudigv You don’t if you’re faking the queue. If the faking the queue, then the job isn’t executed.

So work out what it is you actually want to test: do you want to assert the job was pushed to the queue? Or do you want to assert that the job did what it was supposed to do?

chaudigv's avatar

I need to assert that a Job was dispatched and assert the calculated data after it's completion.

In such case, I can have two tests. One to assert the Job using Queue:fake(). And another one without the fake so it actually runs and I can assert the calculated values.

CorvS's avatar

@chaudigv You probably want to (unit) test whatever your job is doing and test that your job is being dispatched. Everything in between is already tested in the framework itself.

akhaled's avatar

I am doing this:

Queue::assertPushed(MyJob::class, function ($job) {
    $job->handle();
    return true;
});
2 likes

Please or to participate in this conversation.