Framework: $response->assertJsonCount() issue

Created on 2 Feb 2018  路  2Comments  路  Source: laravel/framework

  • Laravel Version: v5.5.33
  • PHP Version: PHP 7.1.6-1~ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jun 9 2017 08:26:34) ( NTS )
  • Database Driver & Version: Server version: 10.1.28-MariaDB-1~xenial mariadb.org binary distribution

Description:

I've got a method in a controller that collects data from the database and sends it to a resource class before returning it to the user as JSON. When I inspect the data in Paw or using dd() it shows all of the expected records in data[0]['records'].

However, when I create 25 instances of the model using factory(Model::class, 25)->create() and verify the count using ->assertJsonCount(25, 'data.*.records'); it shows that it has only one record.

When I dd() before the assertion (after the creation of the data) I see the valid data structure.
When I dd($response->json()['data'][0]['records']) I can see the records array with all 25 items.

In TestResponse.php, line 476 I dump dd(data_get($this->json(), $key)); and see that the data is encased within an array that was not there before!

What is going on?

Steps To Reproduce:

  • Setup a controller method that puts a relation into an nested array
  • Create 25 records using a factory
  • Assert json count in a nested array
  • See it fails in assertJsonCount

Error:

There was 1 failure:

1) Tests\Feature\RecordTest::a_user_can_view_the_records
Failed to assert that the response count matched the expected 25
Failed asserting that actual size 1 matches expected size 25.

/home/vagrant/dev/api/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:481
/home/vagrant/dev/api/tests/Feature/RecordTest.php:65

Most helpful comment

Do $this->assertJsonCount(25, 'data.0.records');

All 2 comments

Do $this->assertJsonCount(25, 'data.0.records');

That fixed the issue, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

digirew picture digirew  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

ghost picture ghost  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments