1
2

More than 3 years have passed since last update.

Laravel6でテストコードを書いてみよう

Posted at

Laravelでは、PHPUnitが標準で組み込まれています。
なのでセットアップ完了後からすぐにテストコードを書き始めることができます!

image.png

「何故テストコードを書かないといけないのか」に関しては、他の方の記事が素晴らしいのでこちらを見ていただければと思います。

FeatureUnitの使い分け

tests配下に、FeatureUnitのディレクトリがあります。

FeatureにはControllerやRouting、Middlewareなどの機能テストを実装します。
それ以外の単体テストをUnitで実装します。

テストの作成

ターミナル
// Featureディレクトリにテストを作成する
$ php artisan make:test UserTest

// Unitディレクトリにテストを作成する
$ php artisan make:test UserTest --unit

テストの実行

ターミナル
// Featureディレクトリのテストを実行する
$ vendor/bin/phpunit tests/Feature/UserTest

// Utilディレクトリのテストを実行する
$ vendor/bin/phpunit tests/Util/UserTest

// 全体をテストするときは以下でもOK
$ php artisan test

ケーススタディ

①ビューのテスト

$response = $this->get('/')で、'/'にアクセスした結果のレスポンスを取得できます。

そして、$response->assertStatus(200)で、そのレスポンスが問題ないこと(ステータスが200 OKであること)をチェックしています。

assertViewIs("welcome")で、welcomeというビューが表示されるかどうかをチェックします。

tests/Feature/RouteTest.php
<?php

namespace Tests\Feature;

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

class RouteTest extends TestCase
{
  public function testRouteTop()
  {
    $response = $this->get("/");
    $response->assertStatus(200)->assertViewIs("welcome");
  }
}

②ログイン状況のテスト

以下は未ログインユーザーのテストです。
$response->assertRedirect("/login");でリダイレクトされ、/loginに移動します。

tests/Feature/RouteTest.php
<?php

namespace Tests\Feature;

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

class RouteTest extends TestCase
{
  public function testGuestCreate()
  {
    $response = $this->get("/create");

    $response->assertRedirect("/login");
  }
}

以下はログインユーザーのテストです。
actingAs($user)でログインします。

tests/Feature/RouteTest.php
<?php

namespace Tests\Feature;

// models
use App\Models\User;

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

class RouteTest extends TestCase
{
  use RefreshDatabase;

  public function testAuthCreate()
  {
    // Userモデルを準備
    $user = factory(User::class)->create();

    // ログインして投稿画面へアクセスすることを実行
    $response = $this->actingAs($user)->get("/create");

    // レスポンスを検証
    $response->assertStatus(200)->assertViewIs("/create");
  }
}

テストのコードの書き方のパターンに「AAA」というのがあります。
Arrage-Act-Assert、日本語でいうと準備・実行・検証となります。

よく使うアサートなどをまとめます。

【assertStatus】レスポンスのステータスが指定したものと正しいか

$response->assertStatus(200);

【assertViewIs】指定したビューが返る

$response->assertViewIs("welcome");

【assertRedirect】指定したURIへリダイレクト

$response->assertRedirect("/login");

【actingAs】ログインする

$response = $this->actingAs($user)

トレイトに関して

コードを再利用するための仕組みをトレイトといいます。
トレイトは、継承と似たPHPの機能です。
汎用性の高いメソッドなどをトレイトとしてまとめておき、他の複数のクラスで共通して使う、という使い方をします。

【RefreshDatabase】データベースに対して行ったアクションをすべて白紙にする

tests/Feature/RouteTest.php
<?php

namespace Tests\Feature;

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

class RouteTest extends TestCase
{
  use RefreshDatabase;

  public function testRouteTop()
  {
    $response = $this->get("/");
    $response->assertStatus(200)->assertViewIs("welcome");
  }
}
1
2
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
1
2