Framework: assertJson() fails where previous seeJson() succeeded

Created on 6 Feb 2017  路  10Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.9
  • PHP Version: 7.1
  • Database Driver & Version:

Description:

assertJson() is failing when it should succeed. seeJson() succeeds. See image below for assertion array and content from response.

Also the failure response from seeJson() was more informative. SeeJson would show you the assertion array along with the content from the response. I had to add a dump() statement in to show both.

Steps To Reproduce:

        $response = $this->get(route('api::bandAvailabilities.index'));
        $response->assertStatus(200);
        dump($response);
        $response->assertJson([
            'title' => 'test',
            'address' => '100 main street',
        ]);

image

Most helpful comment

Sorry for the necromancing. That is great. Thank you pointing out this function. I have spent all day trying to sort this.

@themsaid Is it possible to get assertJsonFragment() added to the documentation so other people don't struggle with an issue which has been solved? I would suggest at https://laravel.com/docs/5.4/http-tests#testing-json-apis

All 10 comments

The core framework doesn't ship with these methods anymore. Please report on the relevant repo.

@GrahamCampbell here is the method in this repo that i am having trouble with
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Testing/TestResponse.php#L195

I think the problem is the new assertJson() method does not work with nesting.

minimal steps to produce:

route::get('testjson', function() {
    return ['data' => ['test' => 'testing']];
});
public function testBasicTest()
{
    $response = $this->get('testjson');

    $response->assertJson(['test' => 'testing']);
}

Fails:

1) Tests\Feature\ExampleTest::testBasicTest
Failed asserting that an array has the subset Array &0 (
    'test' => 'testing'
).

@themsaid Thanks!

Sorry for the necromancing. That is great. Thank you pointing out this function. I have spent all day trying to sort this.

@themsaid Is it possible to get assertJsonFragment() added to the documentation so other people don't struggle with an issue which has been solved? I would suggest at https://laravel.com/docs/5.4/http-tests#testing-json-apis

Hello @themsaid please check this out. Result of using assertJson()

There was 1 failure:

1) Tests\Feature\ViewPropertyTest::testCanViewAProperty
Unable to find JSON:

[{
    "status": "success",
    "data": {
        "id": 1,
        "user_id": "1",
        "title": "Selway Apartments",
        "status": "sale",
        "type": "apartment",
        "price": "470000",
        "area": "1450",
        "rooms": "5",
        "location": "7843 Durham Avenue, MD",
        "created_at": "2017-07-26 23:38:43",
        "updated_at": "2017-07-26 23:38:43",
        "owner": {
            "id": 1,
            "name": "Marquis Rowe",
            "email": "[email protected]",
            "created_at": "2017-07-26 23:38:43",
            "updated_at": "2017-07-26 23:38:43"
        }
    }
}]

within response JSON:

[{
    "status": "success",
    "data": {
        "id": 1,
        "user_id": "1",
        "title": "Selway Apartments",
        "status": "sale",
        "type": "apartment",
        "price": "470000",
        "area": "1450",
        "rooms": "5",
        "location": "7843 Durham Avenue, MD",
        "created_at": "2017-07-26 23:38:43",
        "updated_at": "2017-07-26 23:38:43",
        "owner": {
            "id": 1,
            "name": "Marquis Rowe",
            "email": "[email protected]",
            "created_at": "2017-07-26 23:38:43",
            "updated_at": "2017-07-26 23:38:43"
        }
    }
}].


````

And `assertJsonFragment` method : 

``` php

There was 1 failure:

1) Tests\Feature\ViewPropertyTest::testCanViewAProperty
Unable to find JSON fragment:

["data":{"id":1,"user_id":"1","title":"Selway Apartments","status":"sale","type":"apartment","price":"470000","area":"1450","rooms":"5","location":"7843 Durham Avenue, MD","created_at":"2017-07-26 23:40:44","updated_at":"2017-07-26 23:40:44","owner":{"id":1,"name":"Rigoberto Parisian","email":"[email protected]","created_at":"2017-07-26 23:40:44","updated_at":"2017-07-26 23:40:44"}}]

within

[{"data":{"area":"1450","created_at":"2017-07-26 23:40:44","id":1,"location":"7843 Durham Avenue, MD","owner":{"created_at":"2017-07-26 23:40:44","email":"[email protected]","id":1,"name":"Rigoberto Parisian","updated_at":"2017-07-26 23:40:44"},"price":"470000","rooms":"5","status":"sale","title":"Selway Apartments","type":"apartment","updated_at":"2017-07-26 23:40:44","user_id":"1"},"status":"success"}].
Failed asserting that false is true.


What could possibly be wrong ?

@bahdcasts I have the same problem, looks like a bug to me, assertJson doesnt work neither assertJsonFragment, and I have the exact response that I should be getting

This is my implementation

       $call->assertJson([
           "error" => false,
           "data" => [
               0 => SportPlaceResource::make( $this->cln),
               1 => SportPlaceResource::make( $this->amp),
               2 => SportPlaceResource::make( $this->df)
               ]
        ]);
[{
    "error": false,
    "data": [
        {
            "id": 3,
            "name": "Culiacan",
            "status": "inactive",
            "address": "103 Toy Manor Suite 399\nPort Adrainport, ME 88266",
            "description": null,
            "latlng": "-107.401176,24.792888",
            "invoice": null
        },
        {
            "id": 2,
            "name": "Ubicacion Amplemind",
            "status": "inactive",
            "address": "4287 VonRueden Pike\nLake Justina, VA 80405",
            "description": null,
            "latlng": "-103.36925,20.694316",
            "invoice": null
        },
        {
            "id": 1,
            "name": "Lugar en el DF",
            "status": "inactive",
            "address": "46232 Spinka Radial\nHaleyburgh, MD 64211-2961",
            "description": null,
            "latlng": "-99.18133,19.420327",
            "invoice": null
        }
    ]
}]

within response JSON:

[{
    "error": false,
    "data": [
        {
            "id": 3,
            "name": "Culiacan",
            "status": "inactive",
            "address": "103 Toy Manor Suite 399\nPort Adrainport, ME 88266",
            "description": null,
            "latlng": "-107.401176,24.792888",
            "invoice": null
        },
        {
            "id": 2,
            "name": "Ubicacion Amplemind",
            "status": "inactive",
            "address": "4287 VonRueden Pike\nLake Justina, VA 80405",
            "description": null,
            "latlng": "-103.36925,20.694316",
            "invoice": null
        },
        {
            "id": 1,
            "name": "Lugar en el DF",
            "status": "inactive",
            "address": "46232 Spinka Radial\nHaleyburgh, MD 64211-2961",
            "description": null,
            "latlng": "-99.18133,19.420327",
            "invoice": null
        }
    ]
}]

Failed asserting that an array has the subset Array &0 (
    'error' => false
    'data' => Array &1 (
        0 => App\Http\Resources\SportPlaceResource Object &0000000069874938000000006e5da25b (
            'resource' => App\SportPlace Object &0000000069874aec000000006e5da25b (
                'fillable' => Array &2 (
                    0 => 'status'
                    1 => 'name'
                    2 => 'address'
                    3 => 'description'
                    4 => 'invoice'
                    5 => 'admin'
                )
                'casts' => Array &3 (
                    'invoice' => 'json'
                    'admin' => 'json'
                )
                'spatialFields' => Array &4 (
                    0 => 'latlng'
                )
                'connection' => 'mysql'
                'table' => null
                'primaryKey' => 'id'
                'keyType' => 'int'
                'incrementing' => true
                'with' => Array &5 ()
                'withCount' => Array &6 ()
                'perPage' => 15
                'exists' => true
                'wasRecentlyCreated' => false
                'attributes' => Array &7 (
                    'id' => 3
                    'admin_id' => null
                    'name' => 'Culiacan'
                    'address' => '103 Toy Manor Suite 399\n
Port Adrainport, ME 88266'
                    'description' => null
                    'status' => 'inactive'
                    'invoice' => null
                    'admin' => null
                    'latlng' => Grimzy\LaravelMysqlSpatial\Types\Point Object &0000000069874ad8000000006e5da25b (
                        'lat' => -107.401176
                        'lng' => 24.792888
                    )
                    'created_at' => '2017-10-23 07:50:20'
                    'updated_at' => '2017-10-23 07:50:20'
                )
                'original' => Array &8 (
                    'id' => 3
                    'admin_id' => null
                    'name' => 'Culiacan'
                    'address' => '103 Toy Manor Suite 399\n
Port Adrainport, ME 88266'
                    'description' => null
                    'status' => 'inactive'
                    'invoice' => null
                    'admin' => null
                    'latlng' => Grimzy\LaravelMysqlSpatial\Types\Point Object &0000000069874ad8000000006e5da25b
                    'created_at' => '2017-10-23 07:50:20'
                    'updated_at' => '2017-10-23 07:50:20'
                )
                'changes' => Array &9 ()
                'dates' => Array &10 ()
                'dateFormat' => null
                'appends' => Array &11 ()
                'dispatchesEvents' => Array &12 ()
                'observables' => Array &13 ()
                'relations' => Array &14 ()
                'touches' => Array &15 ()
                'timestamps' => true
                'hidden' => Array &16 ()
                'visible' => Array &17 ()
                'guarded' => Array &18 (
                    0 => '*'
                )
                'geometries' => Array &19 ()
            )
            'with' => Array &20 ()
            'additional' => Array &21 ()
        )
        1 => App\Http\Resources\SportPlaceResource Object &0000000069874935000000006e5da25b (
            'resource' => App\SportPlace Object &0000000069874aeb000000006e5da25b (
                'fillable' => Array &22 (
                    0 => 'status'
                    1 => 'name'
                    2 => 'address'
                    3 => 'description'
                    4 => 'invoice'
                    5 => 'admin'
                )
                'casts' => Array &23 (
                    'invoice' => 'json'
                    'admin' => 'json'
                )
                'spatialFields' => Array &24 (
                    0 => 'latlng'
                )
                'connection' => 'mysql'
                'table' => null
                'primaryKey' => 'id'
                'keyType' => 'int'
                'incrementing' => true
                'with' => Array &25 ()
                'withCount' => Array &26 ()
                'perPage' => 15
                'exists' => true
                'wasRecentlyCreated' => false
                'attributes' => Array &27 (
                    'id' => 2
                    'admin_id' => null
                    'name' => 'Ubicacion Amplemind'
                    'address' => '4287 VonRueden Pike\n
Lake Justina, VA 80405'
                    'description' => null
                    'status' => 'inactive'
                    'invoice' => null
                    'admin' => null
                    'latlng' => Grimzy\LaravelMysqlSpatial\Types\Point Object &0000000069874ada000000006e5da25b (
                        'lat' => -103.36925
                        'lng' => 20.694316
                    )
                    'created_at' => '2017-10-23 07:50:20'
                    'updated_at' => '2017-10-23 07:50:20'
                )
                'original' => Array &28 (
                    'id' => 2
                    'admin_id' => null
                    'name' => 'Ubicacion Amplemind'
                    'address' => '4287 VonRueden Pike\n
Lake Justina, VA 80405'
                    'description' => null
                    'status' => 'inactive'
                    'invoice' => null
                    'admin' => null
                    'latlng' => Grimzy\LaravelMysqlSpatial\Types\Point Object &0000000069874ada000000006e5da25b
                    'created_at' => '2017-10-23 07:50:20'
                    'updated_at' => '2017-10-23 07:50:20'
                )
                'changes' => Array &29 ()
                'dates' => Array &30 ()
                'dateFormat' => null
                'appends' => Array &31 ()
                'dispatchesEvents' => Array &32 ()
                'observables' => Array &33 ()
                'relations' => Array &34 ()
                'touches' => Array &35 ()
                'timestamps' => true
                'hidden' => Array &36 ()
                'visible' => Array &37 ()
                'guarded' => Array &38 (
                    0 => '*'
                )
                'geometries' => Array &39 ()
            )
            'with' => Array &40 ()
            'additional' => Array &41 ()
        )
        2 => App\Http\Resources\SportPlaceResource Object &000000006987493a000000006e5da25b (
            'resource' => App\SportPlace Object &0000000069874ae5000000006e5da25b (
                'fillable' => Array &42 (
                    0 => 'status'
                    1 => 'name'
                    2 => 'address'
                    3 => 'description'
                    4 => 'invoice'
                    5 => 'admin'
                )
                'casts' => Array &43 (
                    'invoice' => 'json'
                    'admin' => 'json'
                )
                'spatialFields' => Array &44 (
                    0 => 'latlng'
                )
                'connection' => 'mysql'
                'table' => null
                'primaryKey' => 'id'
                'keyType' => 'int'
                'incrementing' => true
                'with' => Array &45 ()
                'withCount' => Array &46 ()
                'perPage' => 15
                'exists' => true
                'wasRecentlyCreated' => false
                'attributes' => Array &47 (
                    'id' => 1
                    'admin_id' => null
                    'name' => 'Lugar en el DF'
                    'address' => '46232 Spinka Radial\n
Haleyburgh, MD 64211-2961'
                    'description' => null
                    'status' => 'inactive'
                    'invoice' => null
                    'admin' => null
                    'latlng' => Grimzy\LaravelMysqlSpatial\Types\Point Object &0000000069874ad5000000006e5da25b (
                        'lat' => -99.18133
                        'lng' => 19.420327
                    )
                    'created_at' => '2017-10-23 07:50:20'
                    'updated_at' => '2017-10-23 07:50:20'
                )
                'original' => Array &48 (
                    'id' => 1
                    'admin_id' => null
                    'name' => 'Lugar en el DF'
                    'address' => '46232 Spinka Radial\n
Haleyburgh, MD 64211-2961'
                    'description' => null
                    'status' => 'inactive'
                    'invoice' => null
                    'admin' => null
                    'latlng' => Grimzy\LaravelMysqlSpatial\Types\Point Object &0000000069874ad5000000006e5da25b
                    'created_at' => '2017-10-23 07:50:20'
                    'updated_at' => '2017-10-23 07:50:20'
                )
                'changes' => Array &49 ()
                'dates' => Array &50 ()
                'dateFormat' => null
                'appends' => Array &51 ()
                'dispatchesEvents' => Array &52 ()
                'observables' => Array &53 ()
                'relations' => Array &54 ()
                'touches' => Array &55 ()
                'timestamps' => true
                'hidden' => Array &56 ()
                'visible' => Array &57 ()
                'guarded' => Array &58 (
                    0 => '*'
                )
                'geometries' => Array &59 ()
            )
            'with' => Array &60 ()
            'additional' => Array &61 ()
        )
    )
)

Notice that after the return looks like it tries to match the php code.

I solved it with a simpler approach

        $right_answer = [
           "error" => false,
           "data" => [
               0 => SportPlaceResource::make( $this->cln),
               1 => SportPlaceResource::make( $this->amp),
               2 => SportPlaceResource::make( $this->df)
               ]
        ];

        $this->assertEquals(json_encode($right_answer), $call->getContent());

hope it helps

A workaround for the bug with $response->assertJson($array) is $response->assertSee(json_encode($array)).

A workaround for the bug with $response->assertJson($array) is $response->assertSee(json_encode($array)).

@systemovich your tip saved my life, thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RomainSauvaire picture RomainSauvaire  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

JamborJan picture JamborJan  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

CupOfTea696 picture CupOfTea696  路  3Comments