2
2

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 3 years have passed since last update.

LaravelのPHPUnitのテストでなぜか500エラーが返る時。

Last updated at Posted at 2021-03-20

viewがしっかり200レスポンスを返しているかどうかのテストコード。
テスト関係なく、ブラウザの表示を目で見る限り、問題なく表示され、500レスポンスは返って来ていないのになぜか以下の通りに500レスポンスだと指摘される。

$ ./vendor/bin/phpunit ./tests/Feature/ExampleTest.php
PHPUnit 9.5.3 by Sebastian Bergmann and contributors.

Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

F                                                                   1 / 1 (100%)

Time: 00:00.573, Memory: 20.00 MB

There was 1 failure:

1) Tests\Feature\ExampleTest::testBasicTest
Expected status code 200 but received 500.
Failed asserting that false is true.

/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:186
/var/www/tests/Feature/ExampleTest.php:20

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
ExampleTest.php
<?php

namespace Tests\Feature;

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

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


        $response->assertStatus(200);
    }
}

おかしいなと思いわかったのは、
どうやら、phpunit.xmlも環境に応じて変更する必要があるとのこと。

phpunit.xml
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="mysql"/> #mysqlならmysqlに変更
        <server name="DB_DATABASE" value="〇〇"/> #データベースの名前を入れる
        <server name="MAIL_DRIVER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
    </php>

 できました!!

$ ./vendor/bin/phpunit ./tests/Feature/ExampleTest.php
PHPUnit 9.5.3 by Sebastian Bergmann and contributors.

Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

.                                                                   1 / 1 (100%)

Time: 00:00.492, Memory: 18.00 MB

OK (1 test, 1 assertion)
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?