【问题标题】:Error when running multiple PHPUnit Tests in Laravel 8在 Laravel 8 中运行多个 PHPUnit 测试时出错
【发布时间】:2021-02-02 01:59:54
【问题描述】:

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

导致此错误:

• 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;

【问题讨论】:

  • 你能显示你的 bootstrap/app.php 文件吗? require_once 获取指定文件中存在的所有文本/代码/标记,并将其复制到使用包含语句的文件中。然后你的$app 是函数的返回给你一个true 因为包含可能是成功的。然后,当您尝试从布尔变量调用 make() 时,错误似乎是合乎逻辑的。
  • 我已将应用文件添加到问题正文中。
  • 如果你在var_dump($app)里面DashboardTest.php,它会显示什么?
  • 第一次显示 $app 为 \Illuminate\Foundation\Application ,第二次测试显示 $app 为 true
  • 如果将require_once 替换为require 会怎样?

标签: php laravel unit-testing phpunit laravel-8


【解决方案1】:

因为您运行require_once,该脚本不会再次加载,因此将返回一个布尔值。将require_once替换为require即可解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-09
    • 2017-10-19
    • 2012-03-22
    • 1970-01-01
    • 2017-08-05
    • 2016-03-23
    • 2023-03-24
    • 2016-05-21
    相关资源
    最近更新 更多