{"id":113,"date":"2022-04-19T15:01:20","date_gmt":"2022-04-19T14:01:20","guid":{"rendered":"https:\/\/mrzebra.co.uk\/code\/?p=113"},"modified":"2023-09-20T09:46:16","modified_gmt":"2023-09-20T08:46:16","slug":"laravel-faker-solve-invalidargumentexception-unknown-format","status":"publish","type":"post","link":"https:\/\/zebra-north.com\/code\/2022\/04\/19\/laravel-faker-solve-invalidargumentexception-unknown-format\/","title":{"rendered":"Laravel Faker: Solve InvalidArgumentException: Unknown format &#8220;&#8230;&#8221;"},"content":{"rendered":"\n<p>If you are using Faker in your unit tests then you may encounter an &#8220;Unknown format&#8221; error.  This means that either Faker does recognize the method that you are calling, or that Faker has not been initialized.  <\/p>\n\n\n\n<!--more-->\n\n\n\n<p>The likely cause of this is forgetting to call <code>parent::setUp();<\/code> in your unit test&#8217;s <code>setUp()<\/code> function:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">namespace Tests\\Unit;\n\nuse Tests\\TestCase;\n\nclass MyTest extends TestCase\n{\n    \/**\n     * Called before every test.\n     *\n     * @return void\n     *\/\n    protected function setUp(): void\n    {\n        \/\/ If the line below is missing, Faker will not work.\n        parent::setUp();\n\n        \/\/ ...\n    }\n}<\/code><\/pre>\n\n\n\n<p>If you are trying to use Faker outside of unit tests, then do not use <code>new Generator<\/code>. Instead, create the generator using:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">$faker = Faker\\Factory::create(config(&#039;app.faker_locale&#039;, Faker\\Factory::DEFAULT_LOCALE));<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you are using Faker in your unit tests then you may encounter an &#8220;Unknown format&#8221; error. This means that either Faker does recognize the method that you are calling, or that Faker has not been initialized.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[11],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-php","tag-laravel"],"_links":{"self":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":4,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":198,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/113\/revisions\/198"}],"wp:attachment":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}