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

spkvn's avatar
Level 1

Unable to locate factory with name [default] [App\User]

Hi all,

Going through the testing laravel series, but I'm having trouble running my factory. I get the error:

1) Tests\Feature\BookingTest::testUserRelation InvalidArgumentException: Unable to locate factory with name [default] [App\User].

When running my test. Here's the relevant files:

./database/factories/ModelFactory.php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(\App\User::class, function (Faker\Generator $faker) {
    static $password;
    return [
        'name'           => $faker->name,
        'email'          => $faker->unique()->safeEmail,
        'password'       => $password ?: $password = bcrypt('secret'),
        'category'       => 'Patron',
        'remember_token' => str_random(10)
    ];
});

$factory->define(\App\Room::class, function (Faker\Generator $faker) {
    return [
        'room_number'     => $faker->randomNumber(4,false),
        'beds'            => $faker->randomNumber(1),
        'max_capacity'    => $faker->randomNumber(1),
        'price_per_night' => $faker->randomNumber(3,false)
    ];
});

$factory->define(\App\Booking::class, function(Faker\Generator $faker){
    return[
        'user_id'  => $faker->randomNumber(),
        'room_id'  => $faker->randomNumber(),
        'check_in' => $faker->date('Y-m-d','yesterday'),
        'check_out'=> $faker->date('Y-m-d','tomorrow')
    ];
});

./tests/Feature/BookingTest.php

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

use App\Booking;
use App\Room;
use \App\User;

class BookingTest extends TestCase
{
    use DatabaseTransactions;
    public function setUp()
    {
        factory(User::class)->create();
    /*More code*/
    }
} 

Note that the use statement uses \App\User only as a test, same issue when using App\User;

The factory method runs fine in php artisan tinker, so I'm not sure why It's an issue here.

[123kevin@machine project]$ php artisan tinker
Psy Shell v0.8.11 (PHP 7.1.9 — cli) by Justin Hileman
>>> factory(User::class)->create()
InvalidArgumentException with message 'Unable to locate factory with name [default] [User].'
>>> factory(App\User::class)->create()
=> App\User {#764
     name: "Prof. Ivy Macejkovic",
     email: "[email protected]",
     category: "Patron",
     updated_at: "2017-09-28 05:34:33",
     created_at: "2017-09-28 05:34:33",
     id: 1001,
   }

Let me know what you think.

0 likes
1 reply
spkvn's avatar
spkvn
OP
Best Answer
Level 1

I needed to call parent::setUp() in my test function.

This calls the setUp method in the TestCase Parent Class, which calls CreateApplication, which I think is why my factory methods were not being created.

Should I had parent::setUp() to my own classes' set up or constructor?

4 likes

Please or to participate in this conversation.