7 months ago

Build a Laravel App With TDD - Episode 4, InvalidArgumentException: Unable to locate factory with name [default] [App\Project]

Posted 7 months ago by Perdanjam

I am deep in bug town here, can anyone help? I am following the tutorial line by line and have got as far as the unit test in the episode:

I get the error:

  1. Tests\Unit\ProjectTest::test_it_has_a_path InvalidArgumentException: Unable to locate factory with name [default] [App\Project].

Some info:

The unit test is the second test in the video (starts @ 4:18), my code works fine for the first (feature) test, and the factory function works fine in tinker.

I have tried all kinds of different filepaths for 'App\Project', and also tried static Project::class type references.

My phpunit didn't run tests unless I prefixxed the word 'test', even when filtering to the exact test name, so I believe this may be due to an underlying phpunit version difference.

My unit test:

namespace Tests\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\TestCase;

class ProjectTest extends TestCase

    use RefreshDatabase;

    public function test_it_has_a_path()
        $project = factory('App\Project')->create();
        $this->assertEquals('/project/' . $project->id, $this->path());

My Project factory

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Project;
use Faker\Generator as Faker;

$factory->define(Project::class, function (Faker $faker) {
    return [
        'title' => $faker->sentence,
        'description' => $faker->sentence

My show method

public function show(Project $project)
        return view('', compact('project'));

My route

Route::get('projects/{project}', '[email protected]');

and the working feature test (for reference)

    public function test_a_user_can_view_a_project()
        $project = factory('App\Project')->create();

        $this->get("/projects/" . $project->id)

Please sign in or create an account to participate in this conversation.