在laravel 9,breeze 1.11应用程序中,我想对忘记密码的功能和我发现的路线进行功能测试:
GET|HEAD
In laravel 9, breeze 1.11 app I want to make feature test for forgot-password functionality and in routes I found :
GET|HEAD forgot-password ... password.request › AuthPasswordResetLinkController@create
POST forgot-password ........ password.email › AuthPasswordResetLinkController@store
所以我制作:
测试以检查打开的表单:公共函数testAdminForgetPasswordFormOpened(({$response=$this->get(route('password.request'((;
$response->assertStatus(200);
$response->assertViewIs('auth.forgot-password');
$response->assertSessionHasNoErrors();
}
它工作正常。但当用户提交表单并输入电子邮件时,我没能检查令牌是如何发送的。我知道:
public function testAdminGotPasswordResetLinkEmail()
{
Notification::fake();
$loggedAdmin = User::factory()->make();
$response = $this->post(route('password.email'), [
'email' => $loggedAdmin->email,
]);
$token = DB::table('password_resets')->first();
Notification::assertSentTo(
$loggedAdmin,
SubscriptionEmailingNotification::class,// that is my my Notification class
function ($notification) use ($token) { // https://laravel.com/docs/9.x/mocking#notification-fake
Log::info(varDump($notification, ' -1 $notification::')); / I DO NOT SEE THESE LOG MESSAGES
Log::info(varDump($token, ' -12 $token::'));
return Hash::check($notification->token, $token->token) === true;
}
);
}
但我错了:
1) TestsFeatureAuthTest::testAdminGotPasswordResetLinkEmail
The expected [AppNotificationsSubscriptionEmailingNotification] notification was not sent.
Failed asserting that false is true.
/mnt/_work_sdb8/wwwroot/lar/MngProducts/vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/NotificationFake.php:83
/mnt/_work_sdb8/wwwroot/lar/MngProducts/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:338
/mnt/_work_sdb8/wwwroot/lar/MngProducts/tests/Feature/AuthTest.php:226
看看它是如何在微风中工作的,我看到了方法:
$status = Password::sendResetLink(
$request->only('email')
);
我没有发现上面的方法是如何实现的,它使用了哪个通知?我想这里使用了一些通知,但不确定。。。
我发现assertSentTo方法的声明为:
public static function assertSentTo($notifiable, $notification, $callback = null)
{
必须如何进行测试?
谢谢!
如果继续使用Password::sendResetLink(...)
方法,则不应检查电子邮件中的令牌。Breeze对此已经有了自己的测试。证明Password::sendResetLink(...)
方法被成功调用将足以证实无故障集成。您可以通过检查";重置密码";通知:
Notification::assertSentTo($user, ResetPassword::class);
然而,如果你仍然想检查令牌,你可以使用下面的示例代码,它取自Breeze的测试
use AppModelsUser;
use IlluminateAuthNotificationsResetPassword;
use IlluminateSupportFacadesNotification;
// ...
public function test_password_can_be_reset_with_valid_token()
{
Notification::fake();
$user = User::factory()->create();
$this->post('/forgot-password', ['email' => $user->email]);
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
$response = $this->post('/reset-password', [
'token' => $notification->token,
'email' => $user->email,
'password' => 'password',
'password_confirmation' => 'password',
]);
$response->assertSessionHasNoErrors();
return true;
});
}
来源:https://github.com/laravel/breeze/blob/v1.11.0/stubs/default/tests/Feature/PasswordResetTest.php#L50