assertJobWasAdded() to the batch #37279
Unanswered
awebartisan
asked this question in
Ideas
Replies: 2 comments
-
Would really love to see this added to Laravel, it's a serious road block for Unit/Integration testing. |
Beta Was this translation helpful? Give feedback.
0 replies
-
An interim solution is to create a partial mock of your job class. Here's a simple example using Pest: $jobs = collect();
/** @var MockInterface|MyJob */
$myJob = mock(MyJob::class);
$myJob->shouldReceive('batch')->andReturn($jobs);
$myJob->makePartial();
$myJob->handle();
expect($jobs->flatten()->count())->toBe($expected_batches); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Job Batching offers a way to dynamically add new jobs to the Batch by using
$this->batch()->add()
. When writing tests for the code, we can make sure that the job was added by countingtotalJobs
on that Batch.$this->assertEquals(2, $batch->totalJobs);
The real pain point here is that when we call
add()
method it also executes the given Job. So, in our tests we'll also have to cover the logic of next job. What if the next job makes an API call, we'll need to mock that too.We just wanted to test that a new job was added to the batch.
Hope it makes sense. Looking forward to your thoughts on it :)
Beta Was this translation helpful? Give feedback.
All reactions