[8.x] Added Event::assertAttached to EventFake class#36690
Conversation
8ff3710 to
8b01f79
Compare
|
@luisdalmolin Hey...I'm a little late to this, but I'm trying to use When I explicitly define/declare the listener, then Am I missing something? |
|
@telkins Hmm, I actually didn't test this specifically. I assume it's because the framework didn't "boot" at the point it ran the event discovery, but I'll take a look at this. |
|
@telkins So, auto-discovered listeners get registered with the Event::fake();
Event::assertListening(
Registered::class,
SendEmailVerificationNotification::class.'@handle'
); |
@luisdalmolin Thx. I believe that I'm doing exactly what you wrote...but "[w]hen I explicitly define/declare the listener, then I'll take another swing and get back to you. 🤓 |
@luisdalmolin OK. I see the problem. When I do it like it says in the docs (https://laravel.com/docs/8.x/mocking#event-fake) or either of the two ways you suggested in your initial PR comment, then it fails. When I do it like you posted just recently, it works. So... Event::fake();
Event::assertListening(
Registered::class,
// SendEmailVerificationNotification::class // <-- fails...
// [SendEmailVerificationNotification::class, 'handle'] // <-- fails, even though this is how it's shown in the docs...
SendEmailVerificationNotification::class.'@handle' // <-- works, although this isn't now it's show in the docs...
);So...the docs are wrong...? 🤔 |
This PR adds the ability to assert in unit tests that your event listeners are attached to the expected events. This is something that currently there's not a simple way to test, and it can be a missing gap if you are using event fake in your integration tests and unit testing your event listeners individually.
Some examples of usage:
This was added into the
EventFakeclass, so it's required thatEvent::fake()is called in order to use it.