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);
}
}