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

vidhyaprakash85's avatar

laravel pest mail testing

I am using laravel pest testing

test('can able to submit the form with correct credentials (username / email)', function () {
    $response = $this->get(route('password.request'));
    $response->assertStatus(200);

    $data = [
        'username' => '22j1922',
    ];
    $user = User::where('username', $data['username'])->first();
    $response = $this->post(route('password.email'), $data);

    //1. check whether password reset request is stored in database
    $this->assertDatabaseHas('password_resets', [
        'email' => $user->email,
    ]);
    
    Mail::fake();
    Mail::assertSent(ForgetPasswordEmail::class);

    $response->assertSessionHas('success', 'We have e-mailed your password reset link!');
    $response->assertRedirect(route('password.request'));
});

I am getting this error

Tests\Feature\Auth\ForgetPasswordTest > can able to submit the form with correct credentia…   
  The expected [App\Mail\ForgetPasswordEmail] mailable was not sent.
Failed asserting that false is true.
0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

Move Mail::fake() the top of the test; you want to mock the Mail facade before you perform the actions that cause a mailable to be sent (i.e. the POST request)

1 like

Please or to participate in this conversation.