Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

User::factory() => A facade root has not been set.

Hello,

I have this test.

public User $user;

public function setUp(): void
{
    $this->user = User::factory()->create();
}

public function testItCalculatesAFixedCommission(): void
{
    dd($this->admin);

    $response = $this->get('/');

    $response->assertStatus(200);
}

And I get this erroir.

   FAILED  Tests\Feature\Services\SaleServiceTest > it calculates a fixed commission                                                                                                     RuntimeException   
  A facade root has not been set.

  at vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:355
    351▕     {
    352▕         $instance = static::getFacadeRoot();
    353▕ 
    354▕         if (! $instance) {
  ➜ 355▕             throw new RuntimeException('A facade root has not been set.');
    356▕         }
    357▕ 
    358▕         return $instance->$method(...$args);
    359▕     }

      +1 vendor frames 
  2   database/factories/UserFactory.php:32
      +7 vendor frames 
  10  tests/Feature/Services/SaleServiceTest.php:16


  Tests:    1 failed (0 assertions)
  Duration: 0.12s

UserFactory line 32

'password' => static::$password ??= Hash::make('password'),

Why do I get this error ?

Thanks for your help.

V

0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error "A facade root has not been set" typically occurs when you try to use a Laravel facade or feature that requires the application to be bootstrapped, but the application is not properly set up. In the context of testing, this usually means that the test environment is not being initialized correctly.

Here are a few steps to resolve this issue:

  1. Ensure Proper Test Setup: Make sure you are extending the TestCase class provided by Laravel, which sets up the application environment for testing.

  2. Call parent::setUp(): In your setUp method, ensure you call parent::setUp() to properly initialize the test environment.

  3. Use RefreshDatabase Trait: If your test involves database operations, use the RefreshDatabase trait to ensure the database is migrated and reset for each test.

Here's how you can modify your test class:

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SaleServiceTest extends TestCase
{
    use RefreshDatabase;

    public User $user;

    protected function setUp(): void
    {
        parent::setUp(); // Ensure the parent setup is called

        $this->user = User::factory()->create();
    }

    public function testItCalculatesAFixedCommission(): void
    {
        // Assuming $this->admin should be $this->user
        dd($this->user);

        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

Key Points:

  • Extend TestCase: Ensure your test class extends Tests\TestCase.
  • Call parent::setUp(): This is crucial for setting up the application context.
  • Use RefreshDatabase: This trait helps in resetting the database state between tests.

By following these steps, your test should be able to run without encountering the "A facade root has not been set" error.

Please or to participate in this conversation.