Задать вопрос
@Living4tomorrow

PHPunit тест не видит factory в Laravel, в чём проблема?

Этот тест видит factory для проектов
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');
    }
}

А этот Unit тест выдаёт ошибку
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());
    }
}

текст ошибки
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
use App\Project;
use Faker\Generator as Faker;

$factory->define(Project::class, function (Faker $faker) {
    return [
        'title' => $faker->sentence,
        'body' => $faker->paragraph
    ];
});
  • Вопрос задан
  • 677 просмотров
Подписаться 2 Простой Комментировать
Решения вопроса 1
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы