LoginSignup
6
2

laravel unitテストconfig()が使えない

Last updated at Posted at 2022-06-27

概要

  • laravelのunitテストにてconfig()関数を用いているコードのテストを実行するとconfigクラスが存在しない旨のエラーが出るので解消方法をまとめる。

エラー内容

  • Target class [config] does not exist.

解消方法

  1. 当該のテストコードを開く

  2. 下記のようにCreatesApplicationトレイトをテストクラスの外で名前解決、テストクラス内でuse宣言、setUp()関数を用意して$this->createApplication()を実行する。

    FooTest.php
    <?php
    
    namespace Tests\Unit;
    
    use PHPUnit\Framework\TestCase;
    use Mockery;
    use Tests\CreatesApplication;
    
    class FooTest extends TestCase
    {
        use CreatesApplication;
    
        public function setUp(): void
        {
            parent::setUp();
        
            $this->createApplication();
        }
    }
    

PHPUnitのunitテストのライフサイクルはlaravelの外で行われていることが原因かな・・?

参考文献

6
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
6
2