Laravel 5.5 PHPunit Test - "A facade root has not been set."

Justin Anthony picture Justin Anthony · Sep 27, 2017 · Viewed 11.9k times · Source

When I do a try/catch on the DB::Connection()->getPdo();, I get the error A facade root has not been set. I believe it was happening with the Schema facades too before I tried adding the try/catch. The tests directory is, of course, outside of the app directory, and I have a feeling it has something to do with that, but I have not succeeded in figuring it out.

Here is the test class where this is happening:

<?php

namespace Tests\Models;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
use App\Models\Discussion;
use App\User;
use Business\Database\Model;
use Illuminate\Database\Schema\Blueprint;
use Tests\TestCase;

class DiscussionModelTest extends TestCase
{
    /**
     * Create the tables this model needs for testing.
     */
    public static function setUpBeforeClass()
    {
        try {
            DB::connection()->getPdo();
        } catch(\Exception $e) {
            die($e->getMessage());
        }

        Schema::create('discussions', function (Blueprint $table) {
            $table->integer('id');
            $table->integer('user_id');

            $table->foreign('user_id')->references('id')->on('users');
        });

        Schema::create('users', function (Blueprint $table) {
            $table->integer('id');
        });
    }
}

Answer

Marcin Nabiałek picture Marcin Nabiałek · Sep 27, 2017

The thing is you cannot do this in setUpBeforeClass, because many things are run in setUp method. If you look at this order of run you will see setUpBeforeClass is run before setUp method and TestCase class is doing many things in the setUp method. It looks like this:

protected function setUp()
{
    if (! $this->app) {
        $this->refreshApplication();
    }

    $this->setUpTraits();

    foreach ($this->afterApplicationCreatedCallbacks as $callback) {
        call_user_func($callback);
    }

    Facade::clearResolvedInstances();

    Model::setEventDispatcher($this->app['events']);

    $this->setUpHasRun = true;
}

So what you should do is creating own setUp and tearDown methods with your own implementation like this:

protected function setUp()
{
   parent::setUp();
   // your code goes here
}

protected function tearDown()
{
   parent::tearDown();
   // your code goes here
}