laravel Unable to locate factory with name [default] [App\Models\Domain] 如何解决?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34558098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:56:56  来源:igfitidea点击:

How to solve Unable to locate factory with name [default] [App\Models\Domain]?

laravelunit-testingphpunitlaravel-5.1

提问by alariva

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 Testtrying 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"

我正在使用“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 Domainmight be a conflicting keyword, but that seems not to be the issue.

是的,我做了composer dump-autoload(否则,错误信息会有所不同)

我试图在另一个帮助文件中定义工厂,并且dump-autoload. (以防万一)

我还尝试重命名我的模型,认为这Domain可能是一个有冲突的关键字,但这似乎不是问题。

How may I solve this error?

我该如何解决这个错误?

回答by alariva

Found the problem

发现问题

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

我一直以为我是用文件的工作tests/factories/factories.php,因为我使用laracasts/testdummy

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

事实证明(可能是因为迁移到 L5.1,但不确定),我现在正在使用database/factories/ModelFactory.php它,有一天我更新了我的旧工厂,但从未删除过tests/factories/factories.php,因此,编辑它以进行新的更改毫无价值。

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

现在我已经删除了这个文件,并保留了一个开箱即用的坚持 Laravel 5.1 解决方案的工厂文件。