ハッピーメモメモ

私的備忘録

【Laravel】テストで困ったこと

・これがうまくいかなくて悩んでた…

tests>Feature>AuthTest.php

▼修正前

    /** @test index*/
    public function 正しいパスワードの場合()
    {  
        $response = $this->get('/');
        $response->assertStatus(200);
        // ログインする
        $response = $this->post('/', ['email' => $this->user->email, 'password' => 'abcd1234']);
        // このユーザーがログイン認証されているか
        $this->assertAuthenticatedAs($this->user);
    }

▼修正後

        // ログインする
        $response = $this->post('/api/login', ['email' => $this->user->email, 'password' => 'abcd1234']);

ログインページのURLは「/」だが、

ログインするためには「/api/login」へのPOSTが必要!!

routes>api.php

Route::post('/login', [CookieAuthenticationController::class, 'login']);