Home > Enterprise >  How to test array in AssertableJson in Laravel 8?
How to test array in AssertableJson in Laravel 8?

Time:10-29

In my Laravel project I want to test a JSON response what is acually a paginator result with data key. It's an array and it contains some elements. We don't know the amount exactly, because it's coming from a function.

My problem is if the pagination limit is 10 and I change the elements number over the pagination limit, for example 20, I still get back 10 elements only. But I want to test it too.

Now I have this code:

$response
    ->assertSuccessful()
    ->assertJson(function (AssertableJson $json) use ($allElementsCount) {
        $json
            ->has('data')
            ->whereType('data', 'array')
            // here I want to test the count of 'data' array
            ->etc();
    });

How can I test an array count in a JSON response?

CodePudding user response:

You can use

$response->assertJsonCount(10, 'data');

Reference

  • Related