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

tobz.nz's avatar

Faker in Pest dataset not working

Has anyone used faker in factories that are then used in a Pest dataset?

I get an error: Unknown format "firstName". Directly in the Pest test they work fine, its only in datasets.

Is there something I've missed?

0 likes
6 replies
tobz.nz's avatar

Sure, a tripped down version is:

test:

<?php

it('has orderline index page', function ($user, $status) {
    $this->actingAs($user)
        ->get(route('order-lines.index'))
        ->assertStatus($status);
})->with('users_with_groups');

dataset:

<?php

use App\Group;
use App\User;

dataset('users_with_groups', function () {
    return [
        'No User' => User::factory()->make(),
        'No Permissions' => User::factory()->create([
            'group_id' => Group::factory()->create(['name' => 'no Access'])
        ]),
        'standard' => User::factory()->standard()->create(),
        'products only' => User::factory()->product()->create(),
        'limited' => User::factory()->limited()->create(),
        'readonly' => User::factory()->readonly()->create(),
        'administrator' => User::factory()->administrator()->create(),
    ];
});

If I move the dataset array directly into the ->with() method it works - but as I will use this dataset all over the place it makes sense to use it like its supposed to be used.

...I've just realised this exact setup wont work because I don't have the $status in there, but you get the gist.

Sinnbeck's avatar

@tobz.nz Did you try with bound datasets?

'No User' => fn() => User::factory()->make(),

Your exact example is in the docs I linked

tobz.nz's avatar

@Sinnbeck Yes I've tried that as well. No dice.

Its pretty much exactly the same os in the docs - and if I wasn't using faker in the UserFactory it'd all work fine too.

tobz.nz's avatar

@Sinnbeck Ah.. figured it out - turns out I had not tried bounding it properly. Not sure how I'd tried it before - maybe without yield? Anyway, this worked.

dataset('users_with_groups', function () {
    yield fn () => User::factory()->make();
    ....

Thanks for pointing me in the right direction.

tobz.nz's avatar
tobz.nz
OP
Best Answer
Level 3

Figured it out properly - I was not using the WithFaker class.

<?php

use App\Group;
use App\User;

uses(\Illuminate\Foundation\Testing\WithFaker::class);

dataset('users_with_groups', function () {
    yield 'No Group' => fn () => User::factory()->make();
    yield 'No Access' => fn () => User::factory()->create([
        'group_id' => Group::factory()->create(['name' => 'no Access'])
    ]);
    yield 'Standard' => fn () => User::factory()->standard()->create();
    yield 'Product' => fn () => User::factory()->product()->create();
    yield 'Limited' => fn () => User::factory()->limited()->create();
    yield 'Readonly' => fn () => User::factory()->readonly()->create();
    yield 'Administrator' => fn () => User::factory()->administrator()->create();
});
1 like

Please or to participate in this conversation.