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

Catalinul's avatar

Factory can't be seen in unit test.

This is how my ModelFactory.php looks:

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'email_verified_at' => now(),
        'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        'remember_token' => Str::random(10),
    ];
});

$factory->define(\App\Course::class, function (Faker $faker) {
    return [
        'user_id' => function () {
            return (factory(User::class)->create())->id;
        },
        'name' => $faker->sentence,
        'short_description' => $faker->sentence(20),
        'description' => $faker->paragraph(10),
        'seats' => random_int(0, 10),
        'expiry_date' => $faker->dateTimeBetween('+0 days', '+3 months'),
    ];
});

And this is how my unit test looks:

<?php

namespace Tests\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\TestCase;

class CourseTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    public function a_course_belongs_to_a_teacher() {
        $course = factory(\App\Course::class, 1)->create();

        //$this->assertInstanceOf(\App\Course::class, $course->teacher);
    }
}

This is the error I receive InvalidArgumentException: Unable to locate factory with name [default] [App\Course].

I can't understand why, any help is appreciated.

0 likes
11 replies
Nakov's avatar

@catalinul for this guy the leading backslash was a problem here:

https://stackoverflow.com/a/55207242/1457270

Check if removing the backslash before \App\Course::class clears your problem as well. So it should be App\Course::class instead.

And clear the cache or run composer dump-autoload as well.

Catalinul's avatar

Nothing changed, same error, thanks! If I try the same code in a feature test, it works!

I'm using laravel 6.

Catalinul's avatar

Yes, it works with tinker and it also works when I use it in a feature test, it seems to not be working when using in a unit test, this is a clean installation of laravel 6, I just made it. I tried everything, cleared the cache, I ran composer dump-autoload, I created a new factory, deleted the old one.

If I try:

$user = factory('App\User')->create();

I get exactly the same error in a unit test.

The same code runs if placed in a feature test or used with tinker.

Nakov's avatar
Nakov
Best Answer
Level 73

@catalinul Ah, I see I believe you have the wrong import for the parent class.

Use this

use Tests\TestCase;

instead of

use PHPUnit\Framework\TestCase;

as the first one is where Laravel does all it's startup.

9 likes
bugsysha's avatar

Do you have multiple factories defined in same file? if so then try to split them.

Catalinul's avatar

This is how it was generated by laravel, maybe a bug.

itxshakil's avatar

Yeah this solved my problem but could you please explain the issue in detail.

Please or to participate in this conversation.