-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Event::assertDispatched not asserting anything #18923
Comments
@chrisblackwell Could it be related with #18774? |
It may be because the event fake uses a dispatcher whose listen method does nothing so its unable to speculate that the eloquent.created event has had something registered. But I'm not sure, still digging. |
I'm experiencing the same issue with a custom model event for the Description:Some model events are dispatched with the The Steps To Reproduce:Employee model class Employee extends Model
{
protected $events = [
'updating' => \App\Events\EmployeeUpdate::class,
];
} Employee test class EmployeeTest
{
public function test_update()
{
Event::fake();
$employee = factory(Employee::class)->create();
$employee->update(['name' => 'John']);
Event::assertDispatched(\App\Events\EmployeeUpdate::class);
}
} The "eloquent.booting: App\Employee",
"eloquent.booted: App\Employee",
"eloquent.created: App\Employee",
"eloquent.saved: App\Employee",
"eloquent.updated: App\Employee",
"eloquent.saved: App\Employee", The How to fix:The class EventFake implements Dispatcher
{
public function until($event, $payload = [])
{
return $this->dispatch($event, $payload, true);
}
} |
I have confirmed the |
@chrisblackwell You haven't pointed the |
Also, the The |
@sebdesign I think you're missing the context here. I'm not trying to pickup a Laravel event, I'm asserting that an event fired. Forgetting the missing import in the Also, I believe that |
@chrisblackwell My mistake, I didn't pay attention to the web route. In your feature test, |
@sebdesign It's not event getting to that point though. If you remove the entire line of
|
Isn't that assertion failing because you are dispatching a |
When dispatching events using the `until` method instead of the `dispatch` or `fire` methods, the given events were not being collected in the `events` array of the `EventFake` class. This was resulting false negatives, e.g. `assertDispatched` is failing because the `until` method is not implemented like the `fire` and `dispatch` methods. * Fixes laravel#18923
@sebdesign No, I'm importing the full namespaced path above. Why would it through an exception? |
@chrisblackwell You do import the correct FQCN in your test, but not in your routes file. You model is successfully created in the database, but then an exception is thrown because of the missing event class being dispatched. |
Add in the routes/web.php: Without this line if I try to Then add to
Then your test:
for the test to get success - seems like you need to register the event with the model since the dispatcher compares the event name to the keys of the listed events - then it looks like default factory doesn't add an ID to the user since its a |
@mlantz thanks for your insight! The If you specify the |
When dispatching events using the `until` method instead of the `dispatch` or `fire` methods, the given events were not being collected in the `events` array of the `EventFake` class. This was resulting false negatives, e.g. `assertDispatched` is failing because the `until` method is not implemented like the `fire` and `dispatch` methods. * Fixes #18923
Is it working for anyone ? Its not for me in 8.8.0 |
no not working for me |
it's still not working for me in 9.3 |
This is still an issue. The answer here partially helped https://stackoverflow.com/questions/59861472/laravel-event-dispatch-assertion-fails However, when I used the following code in place of $fake = Event::fake();
DB::setEventDispatcher($fake); It showed me a different error elsewhere in my test. Once I fixed the error, I was able to switch back to just using |
For anyone coming to this in >currentyear, you need to pass the event class you want to check was dispatched into Event::fake(); i.e.
|
Description:
When I try to use the
Event::fake()
and call theEvent::assertDispatched
I get a risky test warning in PHPUnit, saying nothing was asserted.Steps To Reproduce:
I'm running a basic test method for testing my Event:
and in my Prospect model, I just use the
$events
property:The text was updated successfully, but these errors were encountered: