Zebra Codes

Laravel Faker: Solve InvalidArgumentException: Unknown format “…”

19th of April, 2022

If you are using Faker in your unit tests then you may encounter an “Unknown format” error. This means that either Faker does recognize the method that you are calling, or that Faker has not been initialized.

The likely cause of this is forgetting to call parent::setUp(); in your unit test’s setUp() function:

namespace Tests\Unit;

use Tests\TestCase;

class MyTest extends TestCase
{
    /**
     * Called before every test.
     *
     * @return void
     */
    protected function setUp(): void
    {
        // If the line below is missing, Faker will not work.
        parent::setUp();

        // ...
    }
}

If you are trying to use Faker outside of unit tests, then do not use new Generator. Instead, create the generator using:

$faker = Faker\Factory::create(config('app.faker_locale', Faker\Factory::DEFAULT_LOCALE));