Jun 12, 2022
0
Level 3
Pest PHP Factoy
Im using PEST PHP for the first time if I call a factory in a Pest Unit test i get an error.
<?php
use App\Models\Scopes\UserTaskCategoryScope;
use App\Models\TaskCategory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function PHPUnit\Framework\assertCount;
uses(RefreshDatabase::class);
test('scope_gets_only_user_data', function () {
$taskCategory = TaskCategory::factory()->create();
TaskCategory::factory()->create();
TaskCategory::addGlobalScope(new UserTaskCategoryScope($taskCategory->user));
$taskCategories = TaskCategory::all();
assertCount(1, $taskCategories, 'Task Category count is different than expected');
});
Error
• Tests\Unit\UserTaskCategoryScopeTest > scope_gets_only_user_data
InvalidArgumentException
Unknown format "word"
at vendor/fakerphp/faker/src/Faker/Generator.php:723
719▕ return $this->formatters[$format];
720▕ }
721▕ }
722▕
➜ 723▕ throw new \InvalidArgumentException(sprintf('Unknown format "%s"', $format));
724▕ }
725▕
726▕ /**
727▕ * Replaces tokens ('{{ tokenName }}') with the result from the token method call
+2 vendor frames
3 database/factories/TaskCategoryFactory.php:22
Faker\Generator::__call("word", [])
+7 vendor frames
11 tests/Unit/UserTaskCategoryScopeTest.php:13
Illuminate\Database\Eloquent\Factories\Factory::create()
This is my factory
class TaskCategoryFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'user_id' => User::factory(),
'title' => $this->faker->word(),
'color' => $this->faker->hexColor()
];
}
}
EDIT I think I found it out myself
use Tests\TestCase;
uses(TestCase::class);
Please or to participate in this conversation.