Make sure your listeners are wired up

Without this simple test, nothing else matters

Joel Clermont
Joel Clermont
2024-03-11

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!

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼

Level up your Laravel skills!

Each 2-minute email has real-world advice you can use.