Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 21, 2021

Conversation

luisdalmolin
Copy link
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
Copy link
Contributor

telkins commented Mar 29, 2021

@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
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
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
Copy link
Contributor

telkins commented Apr 7, 2021

@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
Copy link
Contributor

telkins commented Apr 7, 2021

@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