phplaravelunit-testingfactoryfaker

Target class [config] does not exist error in laravel for testing


So I am trying to use testing with factories in laravel And I've been getting this error for a long time it's annoying I am trying to make a test with faker by checking if it creates and inserts data into the database.

Target class [config] does not exist.

This is my Test Class

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

use Illuminate\Foundation\Testing\DatabaseMigrations;

use Illuminate\Database\Eloquent\Factories\Factory;

use App\Models\Project;

class CreateProjectTest extends TestCase
{
    /**
     * A basic unit test example.
     *
     * @return void
     */
    public function test_createproject()
    {
        $project = Project::factory()->create();
        $title = $project -> title;
        $this -> assertNotEmpty($title);
    }
}

ProjectFactory

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Project>
 */
class ProjectFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'title' => fake() -> name(),
            'description' => fake() -> sentence,
            'deadline' => fake() -> date('Y_m_d'),
            'status' => fake() -> randomElement(['Proposal', 'In Progress', 'Completed']),
            'client_id' => fake() -> randomElement([2, 3, 5, 8, 9, 13, 16, 17]),
        ];
    }
}

Model\Project I just used fillable

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

use App\Models\User;

class Project extends Model
{
    use HasFactory;

    protected $guarded = [];

    protected $fillable = [
        'title',
        'description',
        'deadline',
        'status',
        'client_id',
    ];

    public function client()
    {
        return $this->belongsTo(User::class, 'client_id', 'id');
    }

    public function userProjects(){
        return $this-> belongsToMany(User::class)->where('is_client',0);
    }
}

Please help this is killing me right now


Solution

  • Anyone wondering how to fix this error I randomly fixed it by replacing this in my Test Class

    use PHPUnit\Framework\TestCase;
    

    with the following

    use Tests\TestCase;
    

    I did this by referring to a video by Code with Dary