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

Ngozistephen's avatar

Default User factory gives InvalidArgumentException: Unknown format "first_name"

Please help me out identifying what I am doing wrong!!! I am getting an error when running my tests. My factory is as below

class UserFactory extends Factory {

/**
 * The name of the factory's corresponding model.
 *
 * @var string
 */

protected $model = User::class;
/**
 * Define the model's default state.
 *
 * @return array
 */
public function definition()
{
    return [
        'first_name' => $this->faker->first_name(),
        'last_name' => $this->faker->last_name(),
        'email' => $this->faker->unique()->safeEmail(),
        'email_verified_at' => now(),
        'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        'remember_token' => Str::random(10),
        'terms_accepted' => (bool)random_int(0, 1),
    ];
}

/**
 * Indicate that the model's email address should be unverified.
 *
 * @return \Illuminate\Database\Eloquent\Factories\Factory
 */
public function unverified()
{
    return $this->state(function (array $attributes) {
        return [
            'email_verified_at' => null,
        ];
    });
}


public function deleted()
{
    return $this->state(function (array $attributes) {
        return [
            'deleted_at' => $this->faker->dateTime('-1 week'),
        ];
    });
}

public function configure()
{
    return $this->afterCreating(function (User $user) {
        $user->assignRole('user');
    });
}

}

and my Test

use RefreshDatabase;

private User $user;

protected function setUp(): void
{
    parent::setUp();

    $this->user = $this->createUser();
}

public function test_paginated_projects_table_doesnt_contain_11th_record()
{
   
    $projects = Project::factory(11)->create();
    $lastproject = $projects->last();
    

    $response = $this->actingAs( $this->user)->get('/projects');

    $response->assertStatus(200);

    $response->assertViewHas('projects', function($collection) use ($lastproject){
        return !$collection->contains($lastproject);
    });
}



private function createUser(): User
{
    return User::factory()->create([
        'terms_accepted' => true
    ]);
}

}

0 likes
2 replies
Nakov's avatar
Nakov
Best Answer
Level 73

Try this:

$this->faker->firstName(),

or just as a property $this->faker->firstName

1 like

Please or to participate in this conversation.