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

headDesk2's avatar

Unit tests - RuntimeException : A facade root has not been set.

Hi, I'm trying to create a factory to use in a unit test.

        $user = factory(User::class, 1)->create()->each(function($u) {
            $u->usersSubscription()
                ->save( factory(UsersSubscription::class)->make() )
                ->each(function($p){
                    $p->usersSubscriptionFeesCollection()->save(factory(UsersSubscriptionFeesCollection::class)->make());
                });
        });

and I get this error :

RuntimeException : A facade root has not been set.

Any help would be gratefully received.

0 likes
7 replies
martinbean's avatar

@headdesk2 If your test is interacting with the database, then it’s not a unit test.

Unit tests extends PHPUnit’s default TestCase so therefore have no access to the Laravel framework (including facades).

Instead, consider creating a feature test instead.

5 likes
headDesk2's avatar

Hi Martin

Sorry for my imprecise language, assuming that I want to do this as a feature test, and I still get this error, what should I do then ?

martinbean's avatar

You shouldn’t, because a feature test would extend the TestCase class in your tests directory instead PHPUnit’s.

Remove:

use PHPUnit\Framework\TestCase;

And replace it with:

use Tests\TestCase;
11 likes
headDesk2's avatar

Thanks Martin, I am very much new to this so appreciate your help.

I now get the following error :

InvalidArgumentException : Unable to locate factory for [App\User].

I have this in database\factories\UserFactory.php


namespace Database\Factories;

use Illuminate\Database\Eloquent\Factory;
use Illuminate\Support\Str;
use App\User;

class UserFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = User::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'first_name' => $this->faker->name,
            'last_name' => $this->faker->name,
            'email' => $this->faker->unique()->safeEmail,
            'email_verified_at' => now(),
            'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    }
}

Any further help would get me out of trouble, many thanks.

martinbean's avatar

@headdesk2 If you’re using the code in your first post, then it’s because you’re mixing the syntax for Laravel 7.x-style factories with the new factory definition syntax introduced in Laravel 8.x.

Be sure to follow the docs for the particular Laravel version you’re using. If you are using Laravel 8.x then take a look at this: https://laravel.com/docs/8.x/database-testing#creating-models-using-factories

You’ll see the syntax for using factories in that version is ModelName::factory()->… instead.

headDesk2's avatar

version 7.x here - I must have not checked the version of the docs before I started taking bits.

Thanks !

1 like
headDesk2's avatar

Martin, if you're still out there... :)

use App\Models\Fees\UsersSubscription;
use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(UsersSubscription::class, function (Faker $faker) {
    return [
            'user_id' => User::Factory(),
            'fee_id' => Fee::Factory(),
            'active' => '1',
            'renew' => '1',
            'tcs_version' => '1',
            'existing_customer' => 1
    ];
});

My factories are 7.x style now. But when I do this...

        $user = factory(User::class)
            ->create()
            ->each(function ($u) {
                $u->usersSubscription()->save(factory(UsersSubscription::class)->make());
            });

I get this

BadMethodCallException : Call to undefined method App\User::Factory()

...but it works if I do this...

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

I should have been a lumberjack...

1 like

Please or to participate in this conversation.