Search code examples
laravelphpunitlaravel-7laravel-testing

Laravel 6/7 testing: A facade root has not been set


I am trying to write some tests for my my Laravel 7 app. But i keep facing A facade root has not been set. error.

<?php

namespace Tests\Feature;

use Illuminate\Support\Facades\Config;
use PHPUnit\Framework\TestCase;

class AddressListenerRepositoryTest extends TestCase
{
    public function testCreate()
    {
        Config::set('x', 'address_listeners'); // Cause of "A facade root has not been set" error
    }
}

Can anybody explain the reason of this error?


Solution

  • You should use use Test\TestCase; instead of PHPUnit\Framework\TestCase

    The test needs to call the createApplication method, which it's located in Test\TestCase and That method will trigger the Kernel so every object and class get bootstrapped. and then you can use the container, facade, request, response and all of that stuff.