Laravel Unit Test: A facade root has not been set


Unit tests are typically meant for performing quick and simple tests. More complex tests with larger dependencies are usually implemented as feature tests in Laravel. However, when using unit tests, it may happen that you need to access elements such as Storage or similar components. When you try to access them, you may encounter the following error message:

A facade root has not been set.

This error occurs because of the default inclusion of:

use PHPUnit\Framework\TestCase;

To be able to use Laravel facades, you need to replace PHPUnit\Framework\TestCase with Tests\TestCase, like this:

use Tests\TestCase;

After making this change, the error message should disappear.