lorisleiva / laravel-actions

⚡️ Laravel components that take care of one specific task
https://laravelactions.com
MIT License
2.52k stars 124 forks source link

Testing batched jobs #270

Closed vpratfr closed 1 month ago

vpratfr commented 8 months ago

Hi,

I got an action which dispatches jobs (which are actions too, hurray). Looks like that:

$documents->each(fn($doc) => GenerateDocumentFiles::dispatch($doc));

and is tested nicely with that

ProcessMissionDeliverable::assertPushedOn('default', 2);

Moving to batches

I wanted to move to batches. So I did that :

$jobs = $documents->map(fn($doc) => GenerateDocumentFiles::makeJob($doc));

Bus::batch($jobs->toArray())->dispatch();

However now my test is failing and saying that no jobs have been dispatched.

Any advices on how to test that?

ismaail commented 3 months ago

see this code of it works for you:

pest test code example

use Illuminate\Bus\ChainedBatch;
use Lorisleiva\Actions\Decorators\JobDecorator;
use Illuminate\Support\Facades\Bus;

it('dispatches jobs', function () {
    Bus::fake();

    Bus::assertChained([
        function (JobDecorator $job) {
            expect($job->getAction())->toBeInstanceOf(FooAction::class);

            return $job;
        },
    ]);
});
Wulfheart commented 1 month ago

Closing due to inactivity. If you feel your issue is still relevant please open a new one with a link to a repository containing a minimal reproducible example.