So, I have an API that returns JSON like so:
"data": [
{
"name": "United Kingdom",
"abbreviation": "UK"
},
{
"name": "United States",
"abbreviation": "US"
},
{
"name": "Australia",
"abbreviation": "AU"
}
]
And I tried to write a test for it. My code is now like this:
<?php
namespace Tests\Feature;
use Tests\DatabaseTestCase;
use App\Countries\Country;
use App\Http\Resources\CountryResource;
class CountryTest extends DatabaseTestCase {
/** @test */
public function it_can_retrieve_a_list_of_countries() {
$country = Country::create([
'name' => 'United Kingdom',
'abbreviation' => 'UK'
]);
$response = $this->json('GET', route('countries.index'), ['data' => $country->toArray()]);
$response->assertJson([
'name' => $country->name,
'abbreviation' => $country->abbreviation
]);
}
}
But the test keeps on failing with the following error:
There was 1 failure:
1) Tests\Feature\CountryTest::it_can_retrieve_a_list_of_countries
Unable to find JSON:
[{
"name": "United Kingdom",
"abbreviation": "UK"
}]
within response JSON:
[{
"data": [
{
"name": "United Kingdom",
"abbreviation": "UK"
}
]
}].
Failed asserting that an array has the subset Array &0 (
'name' => 'United Kingdom'
'abbreviation' => 'UK'
).
--- Expected
+++ Actual
@@ @@
'abbreviation' => 'UK',
),
),
- 'name' => 'United Kingdom',
- 'abbreviation' => 'UK',
)
Does anybody know where the issue is? Thanks.