I have a model that sends an email afterCreate. I'm using Codeception for testing. In order to unit test the model save, I need to disable the event model:afterCreate
. Is there an elegant way to achieve this?
public function afterCreate() {
$this->getDI()
->getShared('mail')
->send([$this->user->email => $this->user->name], 'Reset your password', 'reset', ['resetUrl' => 'reset-password/' . $this->code . '/' . $this->user->email]);
}
In a test for Users model:
public function testRelationToResetPasswords() {
$userId = $this->tester->haveRecord('Myapp\Models\Users', ['name' => 'NameUsedForTesting', 'roleId' => 1, 'email' => '[email protected]']);
$this->tester->haveRecord('Myapp\Models\ResetPasswords', ['usersId' => $userId, 'createdAt' => '0']);
$user = Users::findFirst($userId);
$this->assertGreaterThan(0, count($user->resetPasswords));
}