kenprogrammer's avatar

Assert job dispatched after commit

Is there a way to assert that a job was dispatched after commit?

For example a job like this: ShipOrder::dispatch($order_details)->afterCommit(); When I assert like this: Queue::assertPushed(ShipOrder::class);

I get an error like this: The expected [ShipOrder] job was not pushed. Failed asserting that false is true.

I'm using Pest

0 likes
4 replies
Glukinho's avatar

Was the transaction really committed by the time you assert Queue::assertPushed()?

Glukinho's avatar

@kenprogrammer Maybe your database is tested with RefreshDatabase trait (see here https://laravel.com/docs/12.x/database-testing#resetting-the-database-after-each-test)?

With this trait, all actions with database are executed inside a transaction which is rolled back after the test. And your dispatch()->afterCommit() refers to this "testing" transaction, which is never committed by design, therefore your job is never dispatched in this test.

1 like
kenprogrammer's avatar
kenprogrammer
OP
Best Answer
Level 2

@Glukinho Yes it's like that. I've realized what the issue is after changing to DatabaseTransactions and got the same error. I had not imported the job class in test file. Thanks for the hint. It works now even with RefreshDatabase

Please or to participate in this conversation.