Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zachleigh's avatar

Testing password reset with facade fakes

Im testing the user interface I built to send password reset emails. Im using Selenium with phpunit. Heres my first test:

    /**
     * @test
     */
    public function user_can_reset_password()
    {
        Notification::fake();

        $this->visit('/')
             ->press('Forgot password?')
             ->type($this->user->email(), 'email')
             ->press('Send')
             ->hold()
             ->see(trans('general.email_sent'));

        Notification::assertSentTo($this->user, ResetPassword::class);
    }

Laravel uses notifications to send the password reset email:

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPasswordNotification($token));
    }

So I thought faking Notification would work, but it doesnt. The email is still being sent and the test is failing. I tried faking Mail too, but that didnt work either. Am I doing something wrong?

0 likes
2 replies
superfly's avatar

I'm looking for the same answer, trying to figure out how to test sendPasswordResetNotification().

See https://github.com/laravel/framework/issues/15721#issuecomment-251145812 I found @themsaid 's posts, where he said it should work?!

I guess, it may be the lack of the missing callback paramter in assertSentTo(). When I try to run the test with

Notification::hasSent($user, ResetPassword::class);

the test passed with OK.

    /**
     * Assert if a notification was sent based on a truth-test callback.
     *
     * @param  mixed  $notifiable
     * @param  string  $notification
     * @param  callable|null  $callback
     * @return void
     */
    public function assertSentTo($notifiable, $notification, $callback = null)
JoolsMcFly's avatar

When testing I set MAIL_DRIVER to log and use https://packagist.org/packages/spinen/laravel-mail-assertions

Works like a charm. Example test:

<?php

    use App\Traits\JsonApiHelpers;
    use Illuminate\Foundation\Testing\DatabaseTransactions;
    use Spinen\MailAssertions\MailTracking;

    class MailTest extends TestCase {

        use MailTracking;
        use DatabaseTransactions;
        use JsonApiHelpers;

        public function test_can_send_email_invite() {
            $this->beApiUser();

            $recipient = '[email protected]';
            $json_response = $this->postSecureJson('/api/invite', ['email' => $recipient]);
            $this->assertSuccessfullResponse($json_response);
            $this->seeEmailWasSent();
            $this->seeEmailTo($recipient);
            $this->seeEmailContains("From {$this->user->fullname} ({$this->user->email})");
        }

    }

Please or to participate in this conversation.