尚未设置门面根

A facade root has not been set

我正在为我构建的东西创建我自己的包,其中一个导致手头的问题:尚未设置外观根。当我 运行 我的测试时。

这是路由文件 App\Game\routes\web.php:

<?php
use Illuminate\Support\Facades\Route;

// Core routes for the game related stuff:
Route::get('/game', ['as' => 'game', 'uses' => 'GameController@game']);

非常基本,这里没有什么应该脱颖而出的。那么我们如何注册这些:

<?php

namespace App\Game\Providers;

use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RoutesProvider extends ServiceProvider
{
    protected $namespace = 'App\Game\Controllers';

    public function boot()
    {
        parent::boot();
    }

    public function map()
    {
        $this->mapWebRoutes();
    }

    protected function mapWebRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(__DIR__.'/../routes/web.php');
    }
}

再一次没有什么疯狂的,如果我们将它添加到 config/app.php 和 运行 php artisan route:list 我们看到:

你可以看到上面的游戏路线。所以现在让 运行 我们的测试,确保没有任何问题...

composer phpunit
> ./vendor/bin/phpunit --coverage-html=./test-coverage
PHPUnit 8.4.3 by Sebastian Bergmann and contributors.


Fatal error: Uncaught RuntimeException: A facade root has not been set. in /Users/xxxx/Documents/xxxx/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:242
Stack trace:
#0 /Users/xxxx/Documents/xxxx/app/Game/routes/web.php(5): Illuminate\Support\Facades\Facade::__callStatic('get', Array)
#1 /Users/xxxx/Documents/xxxx/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(955): include_once('/Users/adambala...')
#2 /Users/xxxx/Documents/xxxx/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(239): SebastianBergmann\CodeCoverage\CodeCoverage->initializeData()
#3 /Users/xxxx/Documents/xxxx/vendor/phpunit/phpunit/src/Framework/TestResult.php(646): SebastianBergmann\CodeCoverage\CodeCoverage->start(Object(Tests\Unit\xxxx\BaseStatValueTest))
#4 /Users/xxxx/Documents/xxxx/vendor/phpunit/phpunit/src/Framework/TestCase.php(752): PHPUnit\Framework\TestResult->run(Object(Tests\Unit\xxxx\BaseStatValueTest))
#5 /Users/xxxx/Documents/xxxx/vendor/phpunit/phpunit/src/Framewor in /Users/xxxx/Documents/xxxx/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 242
PHP Fatal error:  Uncaught RuntimeException: A facade root has not been set. in /Users/xxxx/Documents/xxxx/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:242
Stack trace:
#0 /Users/xxxx/Documents/xxxx/app/Game/routes/web.php(5): Illuminate\Support\Facades\Facade::__callStatic('get', Array)
#1 /Users/xxxx/Documents/xxxx/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(955): include_once('/Users/adambala...')
#2 /Users/xxxx/Documents/xxxx/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(239): SebastianBergmann\CodeCoverage\CodeCoverage->initializeData()
#3 /Users/xxxx/Documents/xxxx/vendor/phpunit/phpunit/src/Framework/TestResult.php(646): SebastianBergmann\CodeCoverage\CodeCoverage->start(Object(Tests\Unit\xxxx\BaseStatValueTest))
#4 /Users/xxxx/Documents/xxxx/vendor/phpunit/phpunit/src/Framework/TestCase.php(752): PHPUnit\Framework\TestResult->run(Object(Tests\Unit\xxxx\BaseStatValueTest))
#5 /Users/xxxx/Documents/xxxx/vendor/phpunit/phpunit/src/Framewor in /Users/xxxx/Documents/xxxx/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 242

这一切都是由 web.php 路由文件在尝试创建路由时造成的:Route::get(....)。据我了解,当应用程序启动进行测试时,router 不存在于应用程序容器中。

我试过在我的测试 类 中调用 setUp,并调用它的父级。但我仍然收到错误。

我以前从未遇到过这种情况,互联网对我没有任何帮助。关于我为什么会收到此错误的任何想法?

在 phpunit 覆盖测试中排除路由文件夹:

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>

            <exclude>
                <directory>./app/Game/routes</directory>
            </exclude>
        </whitelist>
    </filter>