tobyreed's avatar

Unable to locate factory with name [default] [App\Reply].

Hello,

I am following Jeffs course series called: "Lets Build a Forum With Laravel" and I am running into an issue I cannot seem to fix even after looking at solutions online :/

The issue I am having is on Episode 3 at around 7:35. He is writing the code like so (and this is what I have too):

namespace Tests\Unit;

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

class ReplyTest extends TestCase
{
    use DatabaseMigrations;
    
    public function test_it_has_owner()
    {
        $reply = factory('App\Reply')->create();

        $this->assertInstanceOf('App\User', $reply->owner);
    }
}

And I run the unit test but get this error: InvalidArgumentException: Unable to locate factory with name [default] [App\Reply]..

I have the factory and I know it exists/works because I am able to use it in php artisan tinker:

Psy Shell v0.9.12 (PHP 7.4.0 — cli) by Justin Hileman
>>> $reply = factory('App\Reply')->create();
=> App\Reply {#3067
     thread_id: 1,
     user_id: 2,
     body: "Cumque blanditiis corrupti ut consequatur doloribus corrupti. Esse cupiditate maxime officiis officia. Voluptatem aut velit dicta molestiae.",
     updated_at: "2019-12-15 19:59:41",
     created_at: "2019-12-15 19:59:41",
     id: 1,
   }
>>>

But for some reason in the test it does not work? I am wondering if someone is able to help/point me in the direction that's able to fix the issue I am having.

Thank you!

0 likes
7 replies
gazd1977's avatar

Dont know if it makes a difference, but in the documentation - it shows referencing the model like this App\Reply::class rather than 'App\Reply'

public function testDatabase()
{
    $user = factory(App\User::class)->make();

    // Use model in tests...
}
tobyreed's avatar

thank you, I tried changing it to App\Reply but it still gives me the same issue :/ I will sleep on it and come back to it tomorrow

gazd1977's avatar

What version of Laravel are you using? Unrelated im sure, but in later versions (from 5.5 onwards i think) the docs have suggested using the RefreshDatabase trait rather than DatabaseMigrations.

1 like
tobyreed's avatar

I am using from what I know Laravel 6 (it's a fresh install yesterday). Also thank you, I will try using that when I get home and see if it helps! :)

Talinon's avatar

@tobyreed From what you posted it doesn't look like you have a setUp() method within your test class, but if you do, you can run into this error if you don't reference the parent's setUp() method.

Example:

class ReplyTest extends TestCase
{

    public function SetUp() : void
    {
        parent::setUp();
    }

}

It wouldn't hurt to try giving a composer dump-autoload a try, too.

tobyreed's avatar
tobyreed
OP
Best Answer
Level 5

I haven't tried this and I was able to fix it last night using a different method.

I was watching the video and looking at the dependencies of the test. It turns out I had imported the wrong Testcase! After importing to correct one it worked.

But from what I assume your solution would have worked as it would have used the parent setUp() method. Also I had already tried composer dump-autoload and it didn't work :/

But thank you all for your solutions. Hope my solution is able to help someone who is following the same series as me and runs into the same issue on L6!

2 likes

Please or to participate in this conversation.