尚未设置立面根

时间:2019-11-23 01:05:15

标签: laravel laravel-6 laravel-facade laravel-testing

  • Laravel 6.5
  • Laravel浏览器套件^ 5.1

我正在为自己构建的东西创建自己的软件包,这导致了当前的问题:尚未设置外观根。当我运行测试时。

这是路由文件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,我们将看到:

enter image description here

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

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,并将其称为父级。但是我仍然得到错误。

我以前从未遇到过,互联网也无济于事。关于我为什么会收到此错误的任何想法?

1 个答案:

答案 0 :(得分:-1)

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

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

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