Open4

Laravel Pestで初回だけシードを実行したい

yussakyussak
beforeAll(function () {
    // データベースをリフレッシュ
    Artisan::call('migrate:fresh');

    // シードを実行
    Artisan::call('db:seed', ['--class' => CreateReportSeeder::class]);
});

A facade root has not been set.となる tests/Pest.phpにはuses(Tests\TestCase::class)->in('Feature', 'Unit');が書いてある

yussakyussak

beforeAllじゃなくbeforeEachをdescribeの外側で実行するので行けた
行けてなさそう

yussakyussak

beforeAll()
Executes the provided closure once before any tests are run within the current file, allowing you to perform any necessary setup or initialization that applies to all tests.

なるほど全テストが対象なのか。今回のはbeforeEachで十分だった

yussakyussak
beforeAll(function () {
    dump("a");
});

describe('Service tests', function () {
    it("あ", function () {
        expect(1 + 1)->toBe(2);
    });
});

以下のようになる 思ってるのと違う

root@5717ef031a2a:/var/www# php artisan test tests/Unit/Services/CreateReportServiceTest.php
^ "a"

   PASS  Tests\Unit\Services\CreateReportServiceTest
  ✓ Service tests → it あ                                                                                                                                                                0.04s  

  Tests:    1 passed (1 assertions)
  Duration: 0.07s

root@5717ef031a2a:/var/www# php artisan test tests/Unit/Services/CreateReportServiceTest.php
^ "a"

   PASS  Tests\Unit\Services\CreateReportServiceTest
  ✓ Service tests → it あ                                                                                                                                                                0.04s  

  Tests:    1 passed (1 assertions)
  Duration: 0.07s

root@5717ef031a2a:/var/www#