Reputation: 3978
The app I am working on fires an event, when one of the Eloquent model attributes is updated.
The Eloquent model is called Job
and it is supposed to fire JobCustomerTotalAmountDueChanged
when the duration
attribute is updated. I have the following code in the JobObserver
:
public function saved(Job $job)
{
if ($job->isDirty('duration')) {
event(new JobCustomerTotalAmountDueChanged($job));
}
}
When I try to test it using Event::fake
, Eloquent events are not being fired, which means that the code in saved
method is never execured. From what I see the assertDispatched
and assertNotDispatched
methods are only available for faked events. Is there a way to assert that a particular event is/is not fired without Event::fake
?
Upvotes: 4
Views: 4604
Reputation: 3978
The solution turned out to be very easy after all:
Laravel lets you specify which events should be faked, as an argument to fake
method. In my example:
Event::fake([JobCustomerTotalAmountDueChanged::class])
All of the other events are being triggered and handled. Also, you can make assertions only with reference to events passed as argument to fake
method. If you don't pass any argument, Laravel will try to 'fake' every event in the app.
Upvotes: 15