Zalo
293
2
Testing

Model Events not trigerring when using factories

Posted 1 year ago by Zalo

Hello all! I'm facing an issue... I don't know why but my model events does not fire when I use factories...

I have this boot:

class App\MyModel extends Model 
{
    protected static function boot()
    {
        parent::boot(); 
        dump('boot');
        static::created(function($replacement) {
            dump('created');
        });
    }
}

And, in my tests, if I write:

MyModel::create([...]); // It fires 'created' event

However, if I use a factory, it doesn't:

factory(App\MyModel::class)->create(); // The boot() method is never called...

What is happening here? I can't test these events in my tests :(

Thx!

PD: I'm using Laravel 5.5

Please sign in or create an account to participate in this conversation.