Skip to content

Unittest does not work when model Auditable #840

@aozen

Description

@aozen
Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 10.~
Package version 13.5.0
PHP version 8.2.7_1

When testing with pest, Pest does not boot the Laravel application. So we may get the error if we use Auditable on our model. Like:

Test.php

Post::factory()->make();

Post.php

use Auditable;

Run test

./vendor/bin/test

Result:

 RuntimeException 

  A facade root has not been set.

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

      +2 vendor frames 

  3   [internal]:0
      App\Models\Post::bootAuditable()
      +10 vendor frames 

  14  tests/Feature/Post/Datasets.php:44
      Illuminate\Database\Eloquent\Factories\Factory::make()

It should be pass the booting app.

it goes the bootAuditable.

public static function bootAuditable()
    {
        if (static::isAuditingEnabled()) {
            static::observe(new AuditableObserver());
        }
    }

then

public static function isAuditingEnabled(): bool
    {
        if (App::runningInConsole()) {
            return Config::get('audit.enabled', true) && Config::get('audit.console', false);
        }

        return Config::get('audit.enabled', true);
    }

In isAuditingEnabled function App::runningInConsole()is failing when using non-boot tools. Because App is not loaded yet. Also Config will not be able to load.

Probably there is not perfect way to fix this. For testing purposes manually I added return true/false; in here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions