【问题标题】:PHPUnit test can't find factoryPHPUnit测试找不到工厂
【发布时间】:2019-07-26 18:37:24
【问题描述】:

我有一个使用JobFactoryClientFactory 的单元测试。

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

class JobTest extends TestCase
{
    /** @test */
    function can_display_job_details()
    {
        $job = create('App\Job', [
            'job-number' => 9999,
            'site' => 'Homeville',
            'client_id' => function(){
                return create('App\Client', [
                    'name' => 'ACME'
                ])->id;
            },

        ]);

        $details = $job->job_details;

        $this->assertEquals('EPS-9999-Homeville-RES', $details);

    }
}

当我运行测试时出现此错误

InvalidArgumentException : Unable to locate factory with name [default] [App\Job].

我在功能测试中使用工厂 App\Job (JobFactory) 没有任何问题。 我使用 PhpStorm 内置的 PHPUnit 测试功能。

我通过一个小测试助手运行 create 和 make

<?php

function create($class, $attributes = [], $times = null)
{
    return factory($class, $times)->create($attributes);
}

function make($class, $attributes = [], $times = null)
{
    return factory($class, $times)->make($attributes);
}

不确定我做错了什么,但我似乎无法深入了解它。 非常感谢任何帮助。

【问题讨论】:

    标签: laravel phpunit phpstorm


    【解决方案1】:

    您扩展了错误的 TestCase 类。

    变化:

    use PHPUnit\Framework\TestCase;
    

    到:

    use Tests\TestCase;
    


    Here你可以找到一个例子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多