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?

More than 1 year has passed since last update.

【Laravel】testing時のエラー【./vendor/bin/phpunit】

Posted at

Laravel開発時の./vendor/bin/phpunitによるエラーまとめ

actingAs()

エラー内容:

There was 1 failure:

1) Tests\Feature\ExampleTest::testBasicTest
Expected response status code [200] but received 302.
Failed asserting that 200 is identical to 302.

原因:auth権限がない状態で$this->get('/posts')などのルーティング
解決策:権限が必要なルーティング前にactingAs()でauth権限を付与しておく

ExampleTest.php
<?php

namespace Tests\Feature;

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

class ExampleTest extends TestCase
{
    use RefreshDatabase;
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->actingAs($this->user())->get('/');

        $response->assertStatus(200);
    }
}

use RefreshDatabase

エラー内容

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: users (SQL: insert into "users" ("name", "email", "email_verified_at", "password", "remember_token", "updated_at", "created_at") values (Hailee Green, langworth.nick@example.com, 2022-06-26 12:31:17, $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi, JQpjdLEH4j, 2022-06-26 12:31:17, 2022-06-26 12:31:17))

原因:使用していたデータベースが影響してusersテーブルでエラーが発生していた?(actingAs()を使用した影響?)
解決策:各Testファイルの先頭にuse RefreshDatabase;を記述

ExampleTest.php
<?php

namespace Tests\Feature;

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

class ExampleTest extends TestCase
{
    use RefreshDatabase;
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->actingAs($this->user())->get('/');

        $response->assertStatus(200);
    }
}

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?