2

I'm going to write some tests for my laravel 7 controller methods (endpoints) to be sure that they fire model observer event (for example saved event on Product model) as you now model observer events do not fire while using mass update and it's possible my teammates use a mass update (by mistake), then any event does not fire and it breaks the remaining processes So by these tests, I can be sure that all parts that rely on model observer events work currently.

In laravel testing document mentioned that if you want to assert an event has been dispatched use this code (for example):

public function testOrderShipping()
{
    Event::fake();

    $order = factory(Order::class)->create();
    // Assert an event was dispatched
    Event::assertDispatched(OrderCreated::class);
}

Is it possible to assert model observer's event has been fired (dispatched) too?

SoheilYou
  • 907
  • 5
  • 23
  • 43
  • To my knowledge, Laravel only registers the events on the observer, it can't determine what observer it is from. – jrcamatog Apr 03 '21 at 16:07
  • 5
    I *think* the event name you should assert was fired is `"eloquent.created: App\Models\Order"`, – IGP Apr 03 '21 at 16:46

0 Answers0