Perdanjam
6 months ago
1713
10
Testing

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

Posted 6 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: https://laracasts.com/series/build-a-laravel-app-with-tdd/episodes/4

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('projects.show', 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()
    {
        $this->withoutExceptionHandling();
        $project = factory('App\Project')->create();

        $this->get("/projects/" . $project->id)
            ->assertSee($project->title)
            ->assertSee($project->description);
    }

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