Модульное тестирование в проекте Laravel 8 выдает ошибку, когда вызывается более одного теста. Проблема заключается в том, что мои тесты не работают, когда я запускаю более одного одновременно. При запуске пакета тестов первый запускается и проходит, как и ожидалось, затем все последующие тесты терпят неудачу при попытке запустить функцию 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
берет весь текст/код/разметку, которые существуют в указанном файле, и копирует их в файл, который использует оператор include. Тогда ваш$app
является возвратом функции, дающей вамtrue
, так как включение, вероятно, было успешным. Тогда ошибка кажется логичной, когда вы пытаетесь вызвать make() из логической переменной. - person Tom Groot   schedule 19.10.2020var_dump($app)
внутриDashboardTest.php
, что это показывает? - person Tom Groot   schedule 19.10.2020\Illuminate\Foundation\Application
, второй тест показывает $app какtrue
- person Reteptt   schedule 19.10.2020require_once
наrequire
? - person Tom Groot   schedule 19.10.2020