1
votes

I am writing phpUnit tests for our application, So for this I wrote a model factory, after that when I try to run the unit test then I am getting an error like " InvalidArgumentException: Unknown formatter 'publicId' ". I have declared table's all column names in my Model factory. Is it required to mention all columns in the factory?

ModelFactory.php

$factory->define(App\Campaign::class, function (Faker\Generator $faker) {
return [
    'public_id' => $faker->publicId,
    'client_id' => $faker->clientID,
    'name' => $faker->name,
    'criteria_age' => $faker->criteriaAge,
    'criteria_state' => $faker->criteriaState,
    'criteria_postcode' => $faker->criteriaPostcode,
    'dncr_required' => $faker->dncrRequired,
    'criteria_state' => $faker->criteriaState,
    'active' => $faker->active,
    'method' => $faker->method,
    'server_parameters' => $faker->serverParameters,
    'parameter_mapping' => $faker->parameterMapping,
];
});

\tests\Unit\Campaign\CampaignTest.php

namespace Tests\Unit\Campaign;

use App\Campaign;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class CampaignTest extends TestCase
{
 use DatabaseTransactions;
 public function testCampaignCreation()
 {
  factory(\App\Campaign::class)->create(['name' => 'tinku']);
  $this->seeInDatabase('campaigns', ['name' => 'tinku']);
 }
}

after running "phpunit tests/Unit/Campaign/CampaignTest.php" I got this error "InvalidArgumentException: Unknown formatter 'publicId'". I am new to Laravel I know there is a procedure to create factories but I couldn't figure out. Hope someone helps. Thanks.

1

1 Answers

4
votes

The formatter is from Faker not Laravel and you can only use Faker formatters Faker ships with.

The error message just tells you that there is no such formatter named publicId. For a list of all Faker formatters please see: https://github.com/fzaninotto/Faker#formatters

If you compare that list with the formatters you've used in your example it becomes more and more obvious that you confused the formatters with some database properties, most likely a translation failure from an existing example? But I think you will know better and this hopefully gives you the information you need to continue setting up your test-case.