如何解决无法找到名称为[default] [App \ Models \ Domain]的工厂? [英] How to solve Unable to locate factory with name [default] [App\Models\Domain]?
问题描述
我正在尝试添加一个Factory,以帮助增加某些控制器上的测试覆盖率.到目前为止,我一直在使用工厂,没有任何问题,在这里我找不到发出此消息的原因,而且我无法弄清与其他工厂的不同之处,它们运作良好.
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.
错误是:
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();
// ...
}
}
<?php
use App\Models\Domain;
trait CreateDomain
{
private function createDomain($overrides = [])
{
return factory(Domain::class)->create($overrides);
}
// ...
}
// ...
$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",
// ...
},
// ...
边注:
是的,我做了
composer dump-autoload
(否则,错误消息将有所不同)
Yes, I did
composer dump-autoload
(Else, the error message would be different)
我试图在另一个帮助程序文件和dump-autoload
中定义工厂. (以防万一)
I tried to define the factory in another helper file, and dump-autoload
. (Just in case)
我还尝试重命名模型,以为Domain
可能是一个有冲突的关键字,但这似乎不是问题.
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
.
I was all the time thinking I was working with the file tests/factories/factories.php
since I was using laracasts/testdummy
.
事实证明(可能是因为迁移到L5.1,但不确定),我现在使用的是database/factories/ModelFactory.php
,我有一天用旧工厂进行了更新,但从未删除过tests/factories/factories.php
,因此无法对其进行编辑因为新的变化是一文不值的.
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.
现在,我已经删除了该文件,并保留了一个符合Laravel 5.1解决方案的出厂文件.
Now I've removed this file and kept a single factory file sticking to Laravel 5.1 solution out of the box.
这篇关于如何解决无法找到名称为[default] [App \ Models \ Domain]的工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!