How to solve Unable to locate factory with name [d

2019-07-12 23:31发布

I'm trying to add a Factory to help increment my test coverage on some controllers. I've been using Factories with no issues till now, where I find no reason for this message and I can't figure out what is different from the rest of the factories, which are working perfectly.

The error is:

1) GuestBusinessControllerTest::it_presents_a_domain_home
InvalidArgumentException: Unable to locate factory with name [default] [App\Models\Domain].

I reference the useful files as follows:

My Controller Test trying to use the Factory (Through a trait)

<?php

use Illuminate\Foundation\Testing\DatabaseTransactions;

class GuestBusinessControllerTest extends TestCase
{
    use DatabaseTransactions;
    use CreateBusiness, CreateDomain;

    // ...

    /** @test */
    public function it_presents_a_domain_home()
    {
        $domain = $this->createDomain();

        // ...
    }
}

The trait using the factory

<?php

use App\Models\Domain;

trait CreateDomain
{
    private function createDomain($overrides = [])
    {
        return factory(Domain::class)->create($overrides);
    }

    // ...

}

The factory definition

// ...

$factory('App\Models\Domain', function ($faker) {
    return [
        'slug'     => str_slug($faker->sentence(3)),
        'owner_id' => 'factory:App\Models\User',
    ];
});

// ...

I'm using "laracasts/testdummy": "~2.0"

// ...

    "require-dev": {
        // ...
        "laracasts/testdummy": "~2.0",
        // ...
    },

// ...

Sidenotes:

Yes, I did composer dump-autoload (Else, the error message would be different)

I tried to define the factory in another helper file, and dump-autoload. (Just in case)

I also tried renaming my model, thinking that Domain might be a conflicting keyword, but that seems not to be the issue.

How may I solve this error?

1条回答
Fickle 薄情
2楼-- · 2019-07-13 00:13

Found the problem

I was all the time thinking I was working with the file tests/factories/factories.php since I was using laracasts/testdummy.

It turns out (probably since the migration to L5.1, but not sure), I was now using database/factories/ModelFactory.php which I one day updated with my old factories, but never removed the tests/factories/factories.php and thus, editing it for new changes was worthless.

Now I've removed this file and kept a single factory file sticking to Laravel 5.1 solution out of the box.

查看更多
登录 后发表回答