【问题标题】:Laravel PHP unit with code coverage giving error PHPUnit\Framework\Exception: PHP Fatal error: Uncaught Error: Class 'Route' not foundLaravel PHP 单元的代码覆盖率给出错误 PHPUnit\Framework\Exception:PHP 致命错误:未捕获错误:找不到类 'Route'
【发布时间】:2020-05-21 00:21:30
【问题描述】:

我收到以下错误

PHPUnit\Framework\Exception:PHP 致命错误:未捕获的错误:在 /var/www/html/checkout/routes/api.php:24 中找不到类 'Route'

我尝试过来自Error: Class 'Route' not found in routes.php when testing standalone Laravel package using Orchestra Testbench 的回答。它不工作并给出同样的错误。

下面是我的 XML 代码:

<filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
            <directory suffix=".php">./routes</directory>
            <exclude>
                <directory >./routes</directory>
                 <file>./routes/api.php</file>
                 <file>./routes/web.php</file>
            </exclude>
        </whitelist>
    </filter>```

【问题讨论】:

标签: php laravel phpunit php-code-coverage


【解决方案1】:

这是我的 PHPUnit 文件

 <filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./app</directory>
        <exclude>
            <directory suffix=".php">/app/Http/Middleware</directory>
            <file>./app/Http/Middleware/RedirectIfAuthenticated.php</file>
        </exclude>
        <exclude>
            <directory suffix=".php">/app/Exceptions</directory>
            <file>./app/Exceptions/Handler.php</file>
        </exclude>
        <exclude>
            <directory suffix=".php">/app/Helpers</directory>
            <file>./app/Helpers/Helpers.php</file>
        </exclude>
        <exclude>
            <directory suffix=".php">/app/Providers</directory>
            <file>./app/Providers/BroadcastServiceProvider.php</file>
            <file>./app/Providers/HttpsProtocolProvider.php</file>
        </exclude>
    </whitelist>
</filter>

【讨论】:

    【解决方案2】:

    我猜它变成了别名。 要解决此问题,请尝试在定义路由的文件中导入 use Illuminate\Support\Facades\Route;

    【讨论】:

    • 在./routes/api.php里面添加了使用Route;收到错误非复合名称'Route'的use语句无效
    • @Roshnijoshi 您指定了全名吗? use Illuminate\Support\Facades\Route;
    【解决方案3】:

    通过从测试用例中删除部分修复

    /**
      * @runInSeparateProcess
     */ 
    

    像下面这样添加了 stderr="true"

    <phpunit backupGlobals="false"
             backupStaticAttributes="false"
             bootstrap="vendor/autoload.php"
             colors="true"
             convertErrorsToExceptions="true"
             convertNoticesToExceptions="true"
             convertWarningsToExceptions="true"
             processIsolation="false"
             stopOnFailure="false"
             stderr="true">
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2018-03-09
      • 2018-04-19
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多