简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM