Framework: $this->faker has no providers loaded in model factories

Created on 9 Sep 2020  路  3Comments  路  Source: laravel/framework


  • Laravel Version: 8.0.0
  • PHP Version: 7.4.2
  • Database Driver & Version: MySQL 5.7.29

Description:

Any attempt to use Model::factory()->create() where $this->faker is used in the definition() method will result in Faker complaining of an unknown formatter. It appears that by default there are no providers loaded.

Steps To Reproduce:

Write a test in a fresh Laravel 8 installation that uses the User factory that ships with it and run it.

public function testBasicTest()
    {
        User::factory()->create();
    }

Results in

 Unknown formatter "name"

  at vendor/fzaninotto/faker/src/Faker/Generator.php:248

Most helpful comment

If you want your unit tests to boot the framework, yes you need to extend Tests\TestCase. 馃憤

All 3 comments

You are attempting to do this in a unit test class. Unit test classes do not boot the framework at all. Use the tests in the Feature test directory.

@taylorotwell @JeffreyWay
this is the source of confussion:
What's new in laravel 8, episode 8

Jeffrey creates a unit test in tests/Unit but use Test/TestCase instead of the PHPUnit\Framework\TestCase used when
php artisan make:test --unit

If you want your unit tests to boot the framework, yes you need to extend Tests\TestCase. 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

shopblocks picture shopblocks  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments