运行 在 Laravel 8 中进行多个 PHPUnit 测试时出错

Error when running multiple PHPUnit Tests in Laravel 8

Laravel 8 项目中的单元测试在调用多个测试时抛出错误。 当我一次 运行 多个时,这个问题导致我的测试失败。当 运行ning 一批测试第一个 运行s 并按预期通过时,然后所有后续测试在尝试 运行 默认 createApplication() 函数时失败 trait CreatesApplication。我的所有测试都已单独 运行 并通过。

导致此错误:

• Tests\Feature\ProjectDashboardTest > dashboard returns200
   Error

  Call to a member function make() on bool

  at tests/CreatesApplication.php:19
     15▕     public function createApplication()
     16▕     {
     17▕         $app = require_once __DIR__.'/../bootstrap/app.php';
     18▕
  ➜  19▕         $app->make(Kernel::class)->bootstrap();
     20▕
     21▕         return $app;
     22▕     }
     23▕ }

这发生在两个测试 运行 相同时。

所以这段代码:

class DashboardTest extends TestCase
{

    public function testDashboardReturns200()
    {
        $response = $this->get('/dashboard');
        $response->assertStatus(200);
    }

    public function testDashboardReturns200V2()
    {
        $response = $this->get('/dashboard');
        $response->assertStatus(200);
    }
}

给出这个结果:


php artisan test ./tests/feature/DashboardTest.php

   FAIL  Tests\Feature\DashboardTest
  ✓ dashboard returns200
  ⨯ dashboard returns200 v2

  ---

  • Tests\Feature\DashboardTest > dashboard returns200 v2
   Error

  Call to a member function make() on bool

  at tests/CreatesApplication.php:19
     15▕     public function createApplication()
     16▕     {
     17▕         $app = require_once __DIR__.'/../bootstrap/app.php';
     18▕
  ➜  19▕         $app->make(Kernel::class)->bootstrap();
     20▕
     21▕         return $app;
     22▕     }
     23▕ }


  Tests:  1 failed, 1 passed
  Time:   4.02s

是否有我遗漏的修复程序?

编辑,按要求添加应用程序文件

<?php

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/

return $app;

因为你 运行 require_once,脚本不会被第二次加载,因此将 return 一个布尔值。将require_once换成require即可解决。