1

When it comes to testing jobs, it seems the solution is simply to instantiate the job and call it's handle method.

As per the Laravel Queue Documentation, you can type hint your dependencies on the Job's handle() method. I'm going to assume you don't do it on the constructor, because that's how the Job's data get's passed in, and the queue workers aren't setup to inject dependencies when they process the job.

So when testing, what is the best way to pass dependencies to the handle method. For now I'm doing the following:

$jobFoo = new jobFoo($bar);
$jobFoo->handle(
  $this->app->make(DependencyExample::class)
);

Is ther a better way to pass dependencies, or perhaps the whole approach to testing jobs this way is flawed.

alexkb
  • 3,216
  • 2
  • 30
  • 30

1 Answers1

2

You can use app to invoke the method and resolve dependencies automatically:

$this->app->call([$jobFoo, 'handle]);
Thiago Barcala
  • 6,463
  • 2
  • 20
  • 23