Last month, I wrote about a clean approach to testing events and listeners.
Those tests will make sure events are emitted and that listeners have the correct internal logic, but what if you forgot to wire up the listener to the event? That would be a problem!
To prevent this mistake, inside each of our listener tests, we also include a simple test that makes sure it is wired up to the expected events:
// tests/Integration/Listeners/SomeListenerTest.php
public function testWiredUpSuccessfully(): void
{
Event::fake();
Event::assertListening(SomeEvent::class, SomeListener::class);
Event::assertListening(SomeOtherEvent::class, SomeListener::class);
}
It's a very simple test to write, but it's important not to forget it.
Here to help,
Joel
P.S. Do you have a burning question about writing Laravel tests? Book a session and ask away!