Summer Sale! All accounts are 50% off this week.

xqus's avatar
Level 8

Default User factory gives InvalidArgumentException: Unknown formatter "name"

Hi,

When running php artisan test the default User factory returns

InvalidArgumentException
Unknown formatter "name"

Using the same factory in for example web.php works fine, it only breaks when it is used in a test.

I have tested in a clean Laravel 8 install, only added $user = User::factory()->create(); to tests\Unit\ExampleTest.php

What am I missing?

0 likes
6 replies
xqus's avatar
Level 8

I figured it out,

I was extending PHPUnit\Framework\TestCase instead of Tests\TestCase.

Doh!

8 likes
ismaile's avatar
ismaile
Best Answer
Level 30

Actually, by default, PHPUnit\Framework\TestCase is used for Unit tests and not only in the ExampleTest.

Indeeed, It will also be the case when you run:

php artisan make:test RandomTest --unit

This is made so that Unit tests can run faster.

The framework isn't booted and you get the speed boost for these types of test: https://github.com/laravel/laravel/pull/5169

On the other hand, Feature tests use Tests\TestCase by default.

5 likes
rvanbaalen's avatar

For anyone else experiencing this issue;

I was in fact extending Tests\TestCase but I failed to call parent::setUp(); in my setUp method which resulted in this same error.

9 likes

Please or to participate in this conversation.