0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravel Featureテスト

Last updated at Posted at 2025-12-01

Featureテスト

Featureテストとは

  • アプリ全体がユーザーの操作どおりに動くかを確認するテストのこと
     
    例;ユーザーがログインフォームを送信したとき、認証が成功し、データベースにセッションが作 成され、指定のページにリダイレクトされるか。
     

配置場所

  • テストファイルは通常、tests/Feature/ ディレクトリ内に配置される。
     

主なテストメソッド

メソッド 役割
$this->get('/posts') GETリクエストを送信
$this->post('/posts',$data) POSTリクエストを送信
$this->actingAs($user) 指定したユーザーとして認証済みの状態のシュミレート
$this->assertStatus(200) レスポンスのHTTPステータスコードを検証
$this->assertRedirect('/home') 指定されたURLにリダイレクトされたことを検証する
$this->assertDatabaseHas('users', $data) データベースに指定したデータが存在するか確認する
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ContactFormTest extends TestCase
{
    // テスト後にDBを初期状態に戻す
    use RefreshDatabase; 

    public function test_contact_form_submission_is_successful(): void
    {
        // 送信するデータ
        $formData = [
            'name' => 'テスト太郎',
            'email' => 'test@example.com',
            'message' => 'お問い合わせのメッセージです。',
        ];

        // POSTリクエストをシミュレーション
        $response = $this->post('/contact', $formData);

        // 成功後にリダイレクトされたか
        $response->assertRedirect('/thanks'); 

        // データベースにデータが保存されたか
        $this->assertDatabaseHas('contacts', [
            'email' => 'test@example.com',
            'name' => 'テスト太郎',
        ]);
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?