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】テストデータの登録をTraitで共通化する方法!

Posted at

泉(@izumin_0401)です。

今回は、Laravelでテストデータの登録を共通化する方法を解説しやす!

ブログ記事はこちら

Laravelでテストデータの登録を共通化する方法

Traitの作成

<?php

namespace Tests\Traits;

trait SeedTestData
{
    public function seedTestData()
    {
        \DB::table('your_table_name')->insert([
            'hoge' => '1',
        ]);
    }
}

データを登録するTraitを作成します。

テストデータでTraitを使う

<?php

namespace Tests\Feature;

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

class SampleTest extends TestCase
{
    use RefreshDatabase, SeedTestData;

    public function testSuccessfulResponse()
    {
        $this->seedTestData();

        $response = $this->get('/api');

        $response->assertOk();
    }
}

テストコードでTraitを使えばOKです。

まとめ

Trait、便利だな〜

ではまた。

最後に

暇つぶしにTwitterブログもやってるので見てね。

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?