mercredi 27 septembre 2017

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

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire