Reset password testing not working Hello,
I'm trying to test my reset password Livewire component, but I noticed that my Password::shouldReceive always passed even if the user not exist.
Maybe I'm doing the wrong way :
// passed but shouldn't.
public function testRequestForgotPasswordWithWrongUser()
{
User::factory([
'email' => '[email protected] '
])->create();
Password::shouldReceive('sendResetLink')
->once()
->with([
'email' => '[email protected] '
])
->andReturn(Password::RESET_LINK_SENT)
;
Livewire::test(ForgotPasswordForm::class)
->set('email', '[email protected] ')
->call('resetAction')
->assertHasNoErrors();
}
Here my Livewire component :
<?php
namespace App\Http\Livewire\Auth;
use Illuminate\Support\Facades\Password;
use Livewire\Component;
use WireUi\Traits\Actions;
class ForgotPasswordForm extends Component
{
use Actions;
public string $email = '';
public function render()
{
return view('livewire.auth.forgot-password-form')
->layout('components.layout');
}
protected function getRules()
{
return [
'email' => 'required|email'
];
}
public function resetAction()
{
$this->validate();
$status = Password::sendResetLink([
'email' => $this->email
]);
if ($status === Password::RESET_LINK_SENT) {
$this->notification()->success(
"Succés",
__($status)
);
$this->fill(['email' => '']);
} else {
$this->notification()->error(
"Une erreur est survenue",
__($status)
);
}
}
}
Hi @deshiloh
Because you always return ->andReturn(Password::RESET_LINK_SENT) in the mock function.
Password::RESET_LINK_SENT = passwords.sent and you want to see Password::INVALID_USER.
What you can do is use 'fake' so the notification email will not be sent.
Something like this.
<?php
public function test_if_password_reset_email_not_is_sent()
{
Notification::fake();
$user = User::factory([
'email' => '[email protected] '
])->create();
Livewire::test(ForgotPasswordForm::class)
->set('email', '[email protected] ')
->call('resetAction')
->assertHasErrors();
Notification::assertNothingSent($user, ResetPassword::class);
}
public function test_if_password_reset_email_is_sent()
{
Notification::fake();
$user = User::factory([
'email' => '[email protected] '
])->create();
Livewire::test(ForgotPasswordForm::class)
->set('email', '[email protected] ')
->call('resetAction')
->assertHasNoErrors();
Notification::assertSentTo($user, ResetPassword::class);
}
Please sign in or create an account to participate in this conversation.