【问题标题】:Laravel 6.12 "A facade root has not been set." when using Log::Laravel 6.12“尚未设置外观根。”使用日志时::
【发布时间】:2020-05-11 18:56:56
【问题描述】:

在单元测试中使用Log 会导致奇怪的错误

版本: Laravel 框架 6.12.0 php 7.4 phpunit 8.5.2

<?php

namespace Tests\Unit;

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

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        Log::info('hellowli');
        $this->assertTrue(true);
    }
}

我跑:

$ php7.4 vendor/bin/phpunit tests/Unit/ExampleTest.php

$ php7.4 vendor/bin/phpunit tests/Unit/ExampleTest.php 
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 49 ms, Memory: 6.00 MB

There was 1 error:

1) Tests\Unit\ExampleTest::testBasicTest
RuntimeException: A facade root has not been set.

/home/toskan/IdeaProjects/apartments/laravel/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:258
/home/toskan/IdeaProjects/apartments/laravel/tests/Unit/ExampleTest.php:17

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

【问题讨论】:

    标签: laravel phpunit laravel-6


    【解决方案1】:

    您的测试用例扩展了错误的基础测试用例。

    只需将命名空间更改为:

    use Tests\TestCase;
    

    代替:

    use PHPUnit\Framework\TestCase;
    

    【讨论】:

    • 我怎么错过了?这是来自 laravel 的“exampletest.php”,内置,就是这样。例如。 ExampleTest.php 从一开始就有错误的导入,直接来自 laravel 安装
    猜你喜欢
    • 2020-05-18
    • 2020-03-23
    • 1970-01-01
    • 2018-03-09
    • 2020-09-10
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多