Skip to content

assertExactJson - False-Positive running #25960

Closed
@sfatihk

Description

@sfatihk
  • Laravel Version: 5.7.7
  • PHP Version: 7.2.9
  • Database Driver & Version: MySQL 5.7.19
  • PHPUnit : 7.3.5

Description:

I saw this error message when testing my City Model resource show route.
Note: Database freshing after test. And i'm using sqlite for testing but i tested this situation on Mysql; anything change.


Error Message

PHPUnit 7.3.5 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 2.46 seconds, Memory: 18.00MB

There was 1 failure:

1) Tests\Unit\app\Http\Controller\Region\CityControllerTest::test_show_func_can_return_true_data
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'{"data":{"name":"West Fannieland","updated_at":"2018-10-05 20:48:11","created_at":"2018-10-05 20:48:11","id":1},"status":"success"}'
+'{"data":{"created_at":"2018-10-05 20:48:11","id":1,"name":"West Fannieland","updated_at":"2018-10-05 20:48:11"},"status":"success"}'

City Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class City extends Model
{
    protected $fillable = ['id','name'];
    
}

City Controller Show function

public function show(City $city)
    {
        return response()
            ->json([
                "status"=>"success",
                "data"=>$city
            ],200);
    }

Test Function

public function test_show_func_can_return_true_data()
    {
        $city = factory(App\City::class)->create();

        $this->json('GET', '/api/cities/'.$city->id)
            ->assertExactJson([
                'status' => 'success',
                'data' => $city
                ]);

    }

Beautified JSON response

Expected
{"data":{
    "name":"West Fannieland",
    "updated_at":"2018-10-05 20:48:11",
    "created_at":"2018-10-05 20:48:11",
    "id":1
},"status":"success"}

Actual
{"data":{
    "created_at":"2018-10-05 20:48:11",
    "id":1,
    "name":"West Fannieland",
    "updated_at":"2018-10-05 20:48:11"
},"status":"success"}

Exactly same data but different sorting. Why it acting like this ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions