And the exact error is?
Sep 17, 2020
7
Level 6
Getting unknown formatter name when running tests
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());
}
}
Level 30
If you need to use a factory in your Unit test, you need to replace:
use PHPUnit\Framework\TestCase;
with:
use Tests\TestCase;
For an explanation: https://laracasts.com/discuss/channels/testing/default-user-factory-gives-invalidargumentexception-unknown-formatter-name
19 likes
Please or to participate in this conversation.