slothentic's avatar

Queue::fake(), assertPushed() with truth test & count not working

Looking through the docs it appears you can assert the job pushed passes a truth test, or you can get the count... but is it possible to do both? It does not appear so from my testing:

Queue::assertPushed(function (ShipOrder $job) use ($order) {
    return $job->order->id === $order->id;
}, 123);

When I use this, the callback param (123) seems to be disregarded when using a truth test as the first condition instead of the class string. I need to use both, to get the number of jobs queued with a given truth test, but not seeing any way to accomplish that.

0 likes
3 replies
Sinnbeck's avatar

Did you try just calling both?

Queue::assertPushed(function (ShipOrder $job) use ($order) {
    return $job->order->id === $order->id;
});
Queue::assertPushed(ShipOrder::class, 123);
slothentic's avatar

Yeah I did, does not work for my case as I need the count filtered for the given truth test.

slothentic's avatar

To give context, I am working on a mechanism to prevent duplicate notifications

For example:

$this->artisan("order:onlySendOnce");
$this->artisan("order:onlySendOnce");

Then check that the specific thing was only dispatched once...

$expectedSentCount = 1;

Queue::assertPushed(function (ShipOrder $job) use ($order) {
    return $job->order->id === $order->id;
}, $expectedSentCount);

Please or to participate in this conversation.