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

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

我正在尝试添加一个工厂来帮助增加我在某些控制器上的测试覆盖率。到目前为止,我一直在使用 Factories,没有任何问题,但我找不到出现此消息的原因,而且我无法弄清楚与其他运行良好的工厂有何不同。

错误是:

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

我参考了有用的文件如下:

我的 Controller Test 尝试使用工厂(通过特征)

<?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();

        // ...
    }
}

trait using the factory

<?php

use App\Models\Domain;

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

    // ...

}

factory definition

// ...

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

// ...

我正在使用 "laracasts/testdummy": "~2.0"

// ...

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

// ...

旁注:

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.

我该如何解决这个错误?

找到问题

自从我使用 laracasts/testdummy.

以来,我一直认为我正在使用文件 tests/factories/factories.php

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

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