Skip to content

[8.x] Added Event::assertAttached to EventFake class#36690

Merged
taylorotwell merged 1 commit into
laravel:8.xfrom
luisdalmolin:event-additional-assertions
Mar 21, 2021
Merged

[8.x] Added Event::assertAttached to EventFake class#36690
taylorotwell merged 1 commit into
laravel:8.xfrom
luisdalmolin:event-additional-assertions

Conversation

@luisdalmolin

Copy link
Copy Markdown
Contributor

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:

Event::fake();

Event::assertAttached(
    Registered::class,
    SendEmailVerificationNotification::class
);

Event::assertAttached(
    Illuminate\Auth\Events\Login::class,
    [UserEventSubscriber::class, 'handleUserLogin']
);

This was added into the EventFake class, so it's required that Event::fake() is called in order to use it.

@luisdalmolin luisdalmolin force-pushed the event-additional-assertions branch from 8ff3710 to 8b01f79 Compare March 21, 2021 12:23
@taylorotwell taylorotwell merged commit 8b01f79 into laravel:8.x Mar 21, 2021
@telkins

telkins commented Mar 29, 2021

Copy link
Copy Markdown
Contributor

@luisdalmolin Hey...I'm a little late to this, but I'm trying to use assertListening on a listener that is "auto-discovered" (https://laravel.com/docs/8.x/events#event-discovery), but it doesn't seem to work.

When I explicitly define/declare the listener, then assertListening passes, but when I remove the explicit definition, then assertListening fails.

Am I missing something?

@luisdalmolin

Copy link
Copy Markdown
Contributor Author

@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.

@luisdalmolin luisdalmolin deleted the event-additional-assertions branch March 30, 2021 14:19
@luisdalmolin

Copy link
Copy Markdown
Contributor Author

@telkins So, auto-discovered listeners get registered with the @handle method in the definition, so you will be able to test them by adding @handle to your assertion. e.g.

Event::fake();

Event::assertListening(
    Registered::class,
    SendEmailVerificationNotification::class.'@handle'
);

@telkins

telkins commented Apr 7, 2021

Copy link
Copy Markdown
Contributor

@telkins So, auto-discovered listeners get registered with the @handle method in the definition, so you will be able to test them by adding @handle to your assertion. e.g.

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 assertListening passes...when I remove the explicit definition, then assertListening fails."

I'll take another swing and get back to you. 🤓

@telkins

telkins commented Apr 7, 2021

Copy link
Copy Markdown
Contributor

@telkins So, auto-discovered listeners get registered with the @handle method in the definition, so you will be able to test them by adding @handle to your assertion. e.g.

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 assertListening passes...when I remove the explicit definition, then assertListening fails."

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...? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants