@ssquare I think it's because you have an empty data in response, like:
data: []
draw: 1
recordsFiltered: 0
recordsTotal: 0
so, it can't really checke what data item looks like
Of course it fales on xdata, because data presented in response
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
This is the sample of response I received on ajax:
data: [
{
action: "some string",
id: 1,
price: 399,
service_name: "Get a Domain",
title: "BASIC",
},
{...},
{...}
]
draw: 1
recordsFiltered: 21
recordsTotal: 21
and my test for this is:
class PackageTest extends TestCase
{
use RefreshDatabase, WithFaker, DatabaseMigrations;
protected $endPoint = '/dashboard/packages';
/**========================
* Should Pass Test Case *
=========================*/
public function test_packages_list_showing_correctly()
{
$this->signIn();
$resp = $this->ajaxGet($this->endPoint)
->assertStatus(Response::HTTP_OK)
->assertJsonStructure([
'data' => [
'*' => [
'xid',
'xtitle',
'xprice',
'xservice_name'
]
],
]);
}
}
Actually, this should have been failed but surprisingly it is being passed. What am I doing wrong here? or from where should I start debugging it.
But, this one is failing, because of xdata
->assertJsonStructure([
'xdata' => [
'*' => [
'xid',
'xtitle',
'xprice',
'xservice_name'
]
],
]);
By the way, if you are curious what does those signin() and ajaxGet(), then here is the code of that:
protected function signIn($user = null)
{
$user = $user ?: User::factory()->create(['role_id' => Role::factory()->create(['name' => 'superAdmin'])]);
$this->actingAs($user, 'web');
return $user;
}
public function ajaxGet($uri)
{
return $this->get($uri, ['HTTP_X-Requested-With' => 'XMLHttpRequest']);
}
@ssquare it do not run seeders. just dd the data from database before ajaxGet
$this->signIn();
dd(Package::count());
$resp = $this->ajaxGet($this->endPoint)
and see how many rows you have in database
Look here about seeds in tests https://laravel.com/docs/7.x/database-testing#using-seeds
Please or to participate in this conversation.