3

Unit testing in a Laravel 8 project is throwing an error when more than one test is called. The issue is causing my tests to fail when I run more than one at once. When running a batch of tests the first one runs and passes as expected, Then all subsequent tests fail when trying to run the createApplication() function from the default trait CreatesApplication. All of my tests have been run individually and pass.

Causing this error:

• 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▕ }

This occurs when the two tests run are identical.

So this code:

class DashboardTest extends TestCase
{

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

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

Gives this result:


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

Is there a fix I am missing?

Edit, Added App file as requested

<?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;
6
  • Can you show your bootstrap/app.php file? require_once takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Then your $app is the return of the function giving you a true as the include was probably successful. Then the error seems logical as you try to call make() from a boolean variable. Commented Oct 19, 2020 at 10:06
  • I've added the app file to the body of the question. Commented Oct 19, 2020 at 10:10
  • If you var_dump($app) inside DashboardTest.php, what does it show? Commented Oct 19, 2020 at 10:17
  • The first time it shows $app as a \Illuminate\Foundation\Application , The second test shows $app as true Commented Oct 19, 2020 at 10:23
  • What if you replace require_once with require? Commented Oct 19, 2020 at 10:39

1 Answer 1

3

Because you run require_once, the script isn't loaded a second time and will therefor return a boolean. Replace require_once with require to solve it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.