-
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 Subscribers failed the tests even the events were fired #20294
Comments
I've found a workaround by overriding the withoutEvents(): /**
* Mock the event dispatcher so all events are silenced and collected.
*
* @return $this
*/
protected function withoutEvents()
{
$mock = Mockery::mock(EventsDispatcherContract::class);
$mock->shouldReceive('fire', 'dispatch')->andReturnUsing(function ($called) {
$this->firedEvents[] = $called;
});
$this->app->instance('events', $mock);
return $this;
} TO: /**
* Mock the event dispatcher so all events are silenced and collected.
*
* @return $this
*/
protected function withoutEvents()
{
$mock = Mockery::mock(EventsDispatcherContract::class);
foreach (['fire', 'until'] as $method) {
$mock->shouldReceive($method, 'dispatch')->andReturnUsing(function ($called) {
$this->firedEvents[] = $called;
});
}
$this->app->instance('events', $mock);
\Illuminate\Database\Eloquent\Model::setEventDispatcher($mock);
return $this;
} Now all my tests are green !!! |
I am having a similar problem with the events not being faked using a User Observer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description:
I want to use the Event Subscribers to manage events & listeners in one place but the tests failed when i call the
expectsEvents
.Steps To Reproduce:
User Model:
Events:
All the user events extend from the base event
UserEvent
:Event Subscriber:
Of course, i've registered the event subscriber in the
EventServiceProvider
.Test
When i ran
phpunit
, this is what i got:The text was updated successfully, but these errors were encountered: