saulsolorzano's avatar

Job not being dispatched in test

Hi!

I'm using laravel 8.5 and laravel Sail.

I'm trying to test a job but is not being dispatched, no matter what I do. Here's my code

Event::fake();
TheProductDoesNotExists::dispatch($this->channel, $document['product'], $document['name']);
Event::assertDispatched(TheProductDoesNotExists::class);

And I get

 The expected [App\Jobs\TheProductDoesNotExists] event was not dispatched.
  Failed asserting that false is true.

Inside the Job I even have a logger in the handle method


public function handle()
    {
		logger('from the job');
		$alertTo = 'test@test';
		Mail::to($alertTo)->send(
			new ProductMissing($this->product, $this->orderName)
		);
    }

And nothing. Any help would be really appreciated! Thanks

0 likes
3 replies
rodrigo.pedra's avatar
Level 56

As TheProductDoesNotExists is a job and not an event you should check with the Bus façade instead of the Event one.

Bus::fake();
TheProductDoesNotExists::dispatch($this->channel, $document['product'], $document['name']);
Bus::assertDispatched(TheProductDoesNotExists::class);

Please or to participate in this conversation.