简体   繁体   English

Laravel 6/7 测试:尚未设置外观根

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

I am trying to write some tests for my my Laravel 7 app.我正在尝试为我的 Laravel 7 应用程序编写一些测试。 But i keep facing A facade root has not been set.但我一直面临着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;您应该使用use Test\TestCase; instead of PHPUnit\Framework\TestCase而不是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.测试需要调用 createApplication 方法,该方法位于Test\TestCase中,该方法将触发 Kernel,因此每个 object 和 class 都会被引导。 and then you can use the container, facade, request, response and all of that stuff.然后你可以使用容器、外观、请求、响应和所有这些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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