PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 12, 2022

[FIXED] How to test array in AssertableJson in Laravel 8?

 February 12, 2022     laravel, laravel-8, laravel-testing, testing     No comments   

Issue

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?


Solution

You can use

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

Reference



Answered By - Professor
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing