lemmon
1 month ago
523
14
Laravel

Unable to locate factory

Posted 1 month ago by lemmon

I am using laravel 6, I am calling a factory from a unit test and php storm just keeps indexing over and over. when I call the test from cmd I get the error: InvalidArgumentException: Unable to locate factory with name [default] [App\Thread].

this happens when I Use $this

<?php

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

use App\User;
use App\Thread;
use Faker\Generator as Faker;

$factory->define(Thread::class, function (Faker $faker) {
    return [
        'user_id' => function () {
            return factory(User::class)->create()->id;
        },
        'title' => $faker->sentence,
        'body' => $faker->paragraph
    ];
});
<?php

namespace Tests\Unit;

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

class ThreadTest extends TestCase
{
    use RefreshDatabase;
    /** @test */
    public function it_has_a_path()
    {
        $thread = factory(Thread::class)->create();

        /*$this->assertEquals("/threads/{$thread->id}", $thread->path());*/
    }
}

If I uncomment the above assertion phpunit will index over and over and not allow the test to work, and when I call the test from cmd it gives me the error

InvalidArgumentException: Unable to locate factory with name [default] [App\Thread].

when I var_dump the $thread it is an instance of the Thread class and is populated???

Please help Thank you

Please sign in or create an account to participate in this conversation.