Black Friday: the Laravel testing course is $149 instead of $249, through Dec 7. See the course →
logo

Make sure your listeners are wired up

Without this simple test, nothing else matters

Joel Clermont
Joel Clermont
2024-03-11

From our daily Laravel tips email. Get the next one in your inbox.

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!

Want a tip like this in your inbox every weekday?

Each one takes less than 2 minutes to read. Join 4,000+ Laravel developers who already get them.

email
No spam. Only real-world advice.
Toss a coin in the jar if you found this helpful.