getTable() on null on Pest Test

Hi all I need some help with some table tests if anyone can spare a few minute please. I am getting:
Error: Call to a member function getTable() on null
Error: Call to a member function getTable() on null
it('can render post titles', function () {
\App\Models\Client::factory()->count(10)->create();

livewire(\App\Filament\Clusters\Contacts\Resources\ClientResource\Pages\ListClients::class)
->assertCanRenderTableColumn('name');
});
it('can render post titles', function () {
\App\Models\Client::factory()->count(10)->create();

livewire(\App\Filament\Clusters\Contacts\Resources\ClientResource\Pages\ListClients::class)
->assertCanRenderTableColumn('name');
});
I must say I am very new to tests. I have used the example on the filamentdocs https://filamentphp.com/docs/3.x/tables/testing The page itself loads okay and shows results. Any advice? EDIT: another example:
it('shows results', function () {
$posts = \App\Models\Client::factory()->count(4)->create();

livewire(\App\Filament\Clusters\Contacts\Resources\ClientResource\Pages\ListClients::class)
->assertCanSeeTableRecords($posts)
->assertCountTableRecords(4);
});
it('shows results', function () {
$posts = \App\Models\Client::factory()->count(4)->create();

livewire(\App\Filament\Clusters\Contacts\Resources\ClientResource\Pages\ListClients::class)
->assertCanSeeTableRecords($posts)
->assertCountTableRecords(4);
});
gives
Error: Call to a member function getTableRecordKey() on null
Error: Call to a member function getTableRecordKey() on null
7 Replies
David | Fortune Validator
it seems any table test fails for some reason
awcodes
awcodes2w ago
Does ->assertSuccessful() work?
David | Fortune Validator
Yes it does. It just seems when I need to test the data on the table I get problems Almost like the data is not being passed into the table for the test I have nothing fancy going on within my table. The factory works and tested looking at table normally. Just the tests not working
Dennis Koch
Dennis Koch2w ago
You didn't log in any user. Usually those weird errors happen when you are not logged in or you are targeting the wrong Panel
David | Fortune Validator
I’ll give this a go and report back. Thanks for the tip to check. I thought I had a global user logged in but I could be wrong. Very new to tests. Out of interest, is it advised to have a testing database rather than my local dev one ? And use the refresh database trait. ? Many thanks So I figured out my issues. 1) needed a user as you said BUT 2) my app service provider defines the gates and each time RefeeshDatabase was run it would loose those each test creates a user with a new permission. Took me an age to figure it out but now I just run the gates again after every user is created per test and it’s okay. Maybe this would be easier with spaties permission package I’m not sure. I just built a light weight one myself.
awcodes
awcodes2w ago
Used this in my Pest.php file on a recent project, use spatie permissions, but maybe some insight.
function loginAsUser(string $role, string $permission, ?User $user = null): User
{
$roleModel = Role::create(['name' => "Manage $role"]);
$view = Permission::create(['name' => "view $permission"]);
$create = Permission::create(['name' => "create $permission"]);
$update = Permission::create(['name' => "update $permission"]);
$delete = Permission::create(['name' => "delete $permission"]);

$roleModel->givePermissionTo([$view, $update, $create, $delete]);

$user = $user ?? User::factory()->create();
$user->assignRole("Manage $role");
actingAs($user);

return $user;
}

function createRole(string $role, string $permission): Role
{
$roleModel = Role::create(['name' => "Manage $role"]);
$view = Permission::create(['name' => "view $permission"]);
$create = Permission::create(['name' => "create $permission"]);
$update = Permission::create(['name' => "update $permission"]);
$delete = Permission::create(['name' => "delete $permission"]);

$roleModel->givePermissionTo([$view, $update, $create, $delete]);

return $roleModel;
}
function loginAsUser(string $role, string $permission, ?User $user = null): User
{
$roleModel = Role::create(['name' => "Manage $role"]);
$view = Permission::create(['name' => "view $permission"]);
$create = Permission::create(['name' => "create $permission"]);
$update = Permission::create(['name' => "update $permission"]);
$delete = Permission::create(['name' => "delete $permission"]);

$roleModel->givePermissionTo([$view, $update, $create, $delete]);

$user = $user ?? User::factory()->create();
$user->assignRole("Manage $role");
actingAs($user);

return $user;
}

function createRole(string $role, string $permission): Role
{
$roleModel = Role::create(['name' => "Manage $role"]);
$view = Permission::create(['name' => "view $permission"]);
$create = Permission::create(['name' => "create $permission"]);
$update = Permission::create(['name' => "update $permission"]);
$delete = Permission::create(['name' => "delete $permission"]);

$roleModel->givePermissionTo([$view, $update, $create, $delete]);

return $roleModel;
}
Seeding the db for tests is possible too, but I like the isolation of being explicit with the data associated with the test.
David | Fortune Validator
Thank you for that. I have a similar setup. I did start with seeding which was a hassle to setup anyway with refresh database trait. But I switched to creating the explicit data per test which I prefer now as well. Appreciate the tips. Tests are a whole new world. It is quite addictive seeing all that green passes though haha

Did you find this page helpful?