Ad

PHPUnit Test Can't Find Factory

- 1 answer

I have a unit test that uses a JobFactory and ClientFactory.

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

    }
}

When I run the test I get this error

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

I use the factory App\Job (JobFactory) in my feature test with no issues. I use PhpStorm's built in PHPUnit testing function.

I run create and make through a little test helper

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

Not sure what I'm doing wrong but I can't seem to get to the bottom of it. Any help greatly appreciated.

Ad

Answer

You are extending the wrong TestCase class.

Change:

use PHPUnit\Framework\TestCase;

to:

use Tests\TestCase;


Here you can find an example.

Ad
source: stackoverflow.com
Ad