Soddashiki's avatar

Laravel testing - can't connect to DB on Unit test (but works perfectly in Feature test)?

I have defined database on phpunit.xml.

On my test I need to connect to database (Schema::getColumnListing('tablename');).

Everything works perfectly if test is in Feature folder, but in Unit folder exactly the same test returns this error:


  at C:\xampp\htdocs\dev\whatever\vendor\laravel\framework\src\Illuminate\Support\Facades\Schema.php:41
     37▕      * @return \Illuminate\Database\Schema\Builder
     38▕      */
     39▕     protected static function getFacadeAccessor()
     40▕     {
  ➜  41▕         return static::$app['db']->connection()->getSchemaBuilder();
     42▕     }
     43▕ }
     44▕

  1   C:\xampp\htdocs\dev\bookus\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:166
      Illuminate\Support\Facades\Schema::getFacadeAccessor()

  2   C:\xampp\htdocs\dev\bookus\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:255
      Illuminate\Support\Facades\Facade::getFacadeRoot()

Should I define database connection for Unit test separately somewhere else (where?) or is there anything else I'm missing here?

Thanks.

0 likes
3 replies
drewdan's avatar

Can you post a redacted version of your phpunit.xml file?

Tray2's avatar
Tray2
Best Answer
Level 74

The two file extends different Testcase classes. The feature test extends Tests/Testcase while the unit test extends PHPunits testcase. Just change the uses clause for the Testcase in your unit test and make it look like it does in your feature test.

2 likes

Please or to participate in this conversation.