lundi 1 juin 2020

PHPUnit assertDataHas() on array

I'm working on a Laravel applications and testing whether my POST request has actually added the record to the database using

$this->assertDatabaseHas('profiles', [
  'physical_address'          => [
   'city'          => $physical_address['city'],
   'line_1'        => $physical_address['line_1'],
   'line_2'        => $physical_address['line_2'],
   'country'       => $physical_address['country'],
   'province'      => $physical_address['province'],
   'postal_code'   => $physical_address['postal_code'],
  ],
]);

I'm making a POST request to the endpoint and passing in.

$physical_address = [
    'line_1'            => $this->faker->secondaryAddress,
    'line_2'            => $this->faker->streetAddress,
    'city'              => $this->faker->city,
    'province'          => $this->faker->state,
    'postal_code'       => $this->faker->postcode,
    'country'           => $this->faker->country,
];

The physical_address column is a JSON column and is added to the $casts array on the model.

The test, however keeps failing with the error message:

1) Tests\Feature\ProfileTest::testCreateProfile
Failed asserting that a row in the table [profiles] matches the attributes {
    "physical_address": {
        "city": "Nicoview",
        "line_1": "Apt. 664",
        "line_2": "136 Mueller Mountain Suite 956",
        "country": "Malta",
        "province": "Arkansas",
        "postal_code": "19176-5371"
    }
}.

When I dump the record that was added to the database, the column structure looks as follows:

"physical_address" => array:6 [
    "city" => "Nicoview"
    "line_1" => "Apt. 664"
    "line_2" => "136 Mueller Mountain Suite 956"
    "country" => "Malta"
    "province" => "Arkansas"
    "postal_code" => "19176-5371"
  ]

I've made sure the keys are in the right order but I don't know what I should try anymore or if I'm missing something obvious.

Aucun commentaire:

Enregistrer un commentaire