簡體   English   中英

Laravel 重置密碼通知未在測試中發送,但確實發送了 email

[英]Laravel Reset Password Notification does not get dispatched in test but does send an email

我正在為我的 Laravel 項目編寫測試。 現在我正在測試登錄、注銷、重置密碼等身份驗證代碼。

可悲的是,我的測試失敗了,因為沒有發送通知。 我嘲笑了通知,但assertSendTo總是失敗,原因The expected [Illuminate\Auth\Notifications\ResetPassword] notification was not sent. .

然而,當實際請求重置密碼 email 時(不是在測試中,作為我網站上的普通用戶)我確實得到了重置密碼 email。因此,它可以正常工作但在我的測試中沒有。 怎么會這樣? .evn也是正確的,我將我的郵件主機設置為mailtrap.io並且我還收到了這個 email... 這是我能給你的最好證明。

這是我的測試:

use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Tests\TestCase;

class AuthTest extends TestCase
{
    /** @test */
    public function a_not_logged_in_user_can_request_a_new_password()
    {
        Notification::fake();

        $email = Str::random() . "@gmail.com";
        $current_password = Str::random(16);
        $current_password_hash = Hash::make($current_password);

        $user = User::factory()->create([
            'email' => $email,
            'password' => $current_password_hash
        ]);

        $response = $this->json('POST', route('password.email'), ['email' => $email]);

        $response->assertStatus(200);
        $response->assertLocation(route('home'));
        
        //$this->expectsNotification($user, ResetPassword::class);
        Notification::assertSentTo($user, ResetPassword::class);
    }
}

任何想法為什么測試不起作用或它有什么問題? 同樣非常奇怪的是,響應代碼200表明一切都成功了,沒有任何問題。

這是我在使用assertSentTo執行測試時得到的錯誤

1) Tests\Feature\Auth\LoggedIn\ForgotPassword\AuthTest::a_not_logged_in_user_can_request_a_new_password
The expected [Illuminate\Auth\Notifications\ResetPassword] notification was not sent.
Failed asserting that false is true.

MyWebsiteProject/vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/NotificationFake.php:68
/MyWebsiteProject/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261
MyWebsiteProject/tests/Feature/Auth/LoggedIn/ForgotPassword/AuthTest.php:35

這是我在使用expectsNotification執行時遇到的錯誤

1) Tests\Feature\Auth\LoggedIn\ForgotPassword\AuthTest::a_logged_in_user_can_request_a_new_password
The following expected notification were not dispatched: [Illuminate\Auth\Notifications\ResetPassword]

MyWebsiteProject/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MocksApplicationServices.php:281
MyWebsiteProject/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:237
MyWebsiteProject/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:153

親切的問候和謝謝!

期望只是“期望某事會發生”,在您的情況下,您在事后期望它。 后者是斷言的用例。 您需要的是以下內容。

class AuthTest extends TestCase
{
    /** @test */
    public function a_not_logged_in_user_can_request_a_new_password()
    {
        $email = Str::random() . "@gmail.com";
        $current_password = Str::random(16);
        $current_password_hash = Hash::make($current_password);

        $user = User::factory()->create([
            'email' => $email,
            'password' => $current_password_hash
        ]);

        // Expect that something is going to happen
        $this->expectsNotification($user, ResetPassword::class);
   
        $response = $this->json('POST', route('password.email'), ['email' => $email]);

        // Assertion that something has happened
        $response->assertStatus(200);
        $response->assertLocation(route('home'));
    }
}

我的應用程序最初是在 L5 中創建的,默認的User.php完全限定 class 名稱是App\User.php但是,\根據 L8 新模式,我錯誤地復制了它(我已經復制了它) App\Models\User.php

https://twitter.com/taylorotwell/status/1296556354593792000

This partial misalignment, though, left some of my tests behind, expecially the ones involving fake-notifications in which UserFactory kept dispatching (via NotificationFake.php ) to App\User.php instead of App\Models\User.php and determining the assertSentTo失敗。

TLDR make sure that config/auth.php (param providers.users.model ) and UserFactory.php rely on the same model.

如果上述解決方案都不適合您,請嘗試在測試 function 的頂部添加此行;

$this->withoutExceptionHandling();

這將為您提供一個很好的錯誤消息,說明問題可能來自何處。 就我而言,我使用了錯誤的路線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM