Writing PEST tests for Filament Tables
Hi, I have a
ListItemResource
which is simply to store a key value pair in the database. I have a ListListItems.php
which includes the table of all the entries. I'm trying to write a test to ensure that the table is populated. So far I have the following tests:
Render pages - Works fine β
Check "value" column is in table - Works fine β
Check all items in table - Fails β
I've tried all sorts of different variations including PHPUnit and PEST. I currently have PEST installed along with the livewire plugin too. The user is authenticated via actingAs
method. All namespaces etc are used.
It seems that the tests just blurts out the HTML to the terminal, but can never match any results from the array of ListItem
s.
The code is as close to the docs here as I can get: https://filamentphp.com/docs/3.x/panels/testing#table but still no joy.
I've attached a screenshot showing the test code and the error in the terminal.
Thanks in advance πSolution:Jump to solution
Are you modifying something in the query? It is weird because your table has 12 records..
try
->assertCountTableRecords(12)
...8 Replies
dd(ListItem::count())
What is the output?10 // tests/Feature/ListItemTest.php:32
10 - looks like they're being createdwhere did you put it?
Right after the create:
Solution
Are you modifying something in the query? It is weird because your table has 12 records..
try
->assertCountTableRecords(12)
So I simplified the test so that it has just one assert - the
PASS Tests\Feature\DashboardTest β can render pages 18.80s
PASS Tests\Feature\ListItemTest β can render pages 1.09s
β can see value columns on table 1.04s
β it check all lists items are shown 1.03s
Tests: 5 passed (28 assertions) Duration: 22.10s
assertCountTableRecords
on. And it's coming back as Failed asserting that 1 is identical to 10
.
Now you mention modifying the query, I set some default filters on the, ListItemResource
so I suspect that will be having an impact.
Aha - removing the default filters made this test pass and also the previous assertCanSeeTableRecords
@php artisan testPASS Tests\Unit\ExampleTest β that true is true 0.01s
PASS Tests\Feature\DashboardTest β can render pages 18.80s
PASS Tests\Feature\ListItemTest β can render pages 1.09s
β can see value columns on table 1.04s
β it check all lists items are shown 1.03s
Tests: 5 passed (28 assertions) Duration: 22.10s
yep, the test is right, the count is different actually
Thanks for your help π