havvtom
1 month ago
209
3
Testing

Getting unknown formatter name when running tests

Posted 1 month ago by havvtom

May you help me, identifying what I am doing wrong!!! I am getting an error when running my tests. My factory is as below

<?php

namespace Database\Factories;

use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class CategoryFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Category::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        
        return [
            'name' => $name = $this->faker->name,
            'parent_id' => 1,
            'order' => 1,
            'slug' => Str::slug($name)
        ];
    }
}

and my test is

<?php

namespace Tests\Unit\Models\Categories;

use PHPUnit\Framework\TestCase;
use App\Models\Category;

class CategoryTest extends TestCase
{
    /**
     * A basic unit test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->assertTrue(true);
    }

    public function test_it_has_many_children()
    {
    	$category = Category::factory()->create(['parent_id' => null]);

    	$category->children()->save([
    		Category::factory()->create(['parent_id' => 1])
    	]);

    	$this->assertInstanceOf(Category::class, $category->children->first());
    }
}

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