#getTable() on null on Pest Test

12 messages · Page 1 of 1 (latest)

oblique stag
#

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

    \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);
});```

gives ```Error: Call to a member function getTableRecordKey() on null```
blissful furnaceBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

oblique stag
#

it seems any table test fails for some reason

void isle
#

Does ->assertSuccessful() work?

oblique stag
#

Almost like the data is not being passed into the table for the test

oblique stag
#

I have nothing fancy going on within my table. The factory works and tested looking at table normally. Just the tests not working

true socket
#

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

oblique stag
oblique stag
#

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.

void isle
# oblique stag So I figured out my issues. 1) needed a user as you said BUT 2) my app servic...

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;
}
void isle
#

Seeding the db for tests is possible too, but I like the isolation of being explicit with the data associated with the test.