L
L
Living4tomorrow2019-12-17 18:12:52
Unit testing
Living4tomorrow, 2019-12-17 18:12:52

PHPunit test doesn't see factory in Laravel, what's the problem?

This test sees a factory for projects

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class ProjectsTest extends TestCase
{
   use RefreshDatabase, WithFaker;

    /** @test */
    public function test_a_user_can_create_a_project()
    {
        $this->withoutExceptionHandling();

        $attributes = [
            'title' => $this->faker->sentence,
            'body' => $this->faker->paragraph
        ];

        $this->post('/projects', $attributes);

        $this->assertDatabaseHas('projects', $attributes);
        $this->get('/projects')->assertSee($attributes['title']);
    }

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

    public function test_a_project_requires_a_title()
    {
        $attributes = factory('App\Project')->raw(['title'=>'']);
        $this->post('/projects', $attributes)->assertSessionHasErrors('title');
    }

    public function test_a_prooject_requires_a_body()
    {
        $attributes = factory('App\Project')->raw(['body'=>'']);
        $this->post('projects', $attributes)->assertSessionHasErrors('body');
    }
}

And this Unit test throws an error
namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ProjectTest extends TestCase
{
    use RefreshDatabase;
    /** @test */
    public function test_it_has_a_path()
    {
        $project = factory('App\Project')->create();
        $this->assertEquals('project/' . $project->id, $project->path());
    }
}

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

C:\xampp\htdocs\birdboard\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:269
C:\xampp\htdocs\birdboard\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:292
C:\xampp\htdocs\birdboard\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\GuardsAttributes.php:122
C:\xampp\htdocs\birdboard\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:300
C:\xampp\htdocs\birdboard\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:219
C:\xampp\htdocs\birdboard\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:178
C:\xampp\htdocs\birdboard\tests\Unit\ProjectTest.php:15

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

$factory->define(Project::class, function (Faker $faker) {
    return [
        'title' => $faker->sentence,
        'body' => $faker->paragraph
    ];
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Derepko, 2019-12-17
@Living4tomorrow

5df8f35170611738756673.png
5df8f359df0b5446053925.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question