Laravel "A facade root has not been set" in CI
A Laravel facade proxies to a service resolved from the application container. If the app was never bootstrapped, the facade has no root to resolve against and throws "A facade root has not been set." In tests this means the case did not boot the framework.
What this error means
A test using a facade (Config, DB, Cache, Log) fails with "RuntimeException: A facade root has not been set." The test extends PHPUnit\Framework\TestCase directly instead of the Laravel base TestCase.
RuntimeException: A facade root has not been set.
/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:358Common causes
The test does not bootstrap the application
A pure unit test extending PHPUnit\Framework\TestCase never calls createApplication(), so no container exists for facades to resolve against.
A facade is used in setup before boot
Code called during test construction touches a facade before the framework has booted, so the root is unset.
How to fix it
Extend the Laravel base TestCase
- Extend
Tests\TestCase, which uses CreatesApplication. - Let it boot the app so facades resolve.
- Re-run the test with the framework bootstrapped.
namespace Tests\Feature;
use Tests\TestCase;
class ReportTest extends TestCase { /* facades work here */ }Avoid facades in true unit tests
For tests that intentionally skip the framework, inject dependencies instead of calling facades.
$service = new ReportService($repository);How to prevent it
- Extend
Tests\TestCasefor anything that touches the framework. - Keep facade usage out of pure unit tests.
- Inject dependencies so unit tests need no container.