When writing an automated test case for Lighthouse 5, assertion that the response has a given JSON structure, that is, only verify whether the field exists, not to verify its value
1. Run the GraphQL Query API and respond 200. The main test field: The responses of TheMeAssets. as shown in Figure 1
2. Add the test field: ThemeAssets . The main validation field exists. Because the value of the field is unpredictable. Nor is it ready to insert a new record. The assertion response has a given JSON structure, based on $response->AssertJsonStructure(array $structure);
public function testGetThemeById(): void
{
$response = $this->graphQL('
query GetThemeById($id: ID!) {
onlineStoreTheme(themeId: $id) {
id
editable
createdAt
name
themeAssets {
id
themeId
version
key
mimeType
category
schema
createdAt
updatedAt
}
}
}
',
[
'id' => 'vogue',
]
);
$response->assertJson(
[
'data' => [
'onlineStoreTheme' => [
'id' => 'vogue'
],
],
]
)->assertJsonStructure([
'data' => [
'onlineStoreTheme' => [
'themeAssets' => [
[
'id',
'themeId',
'version',
'key',
'mimeType',
'category',
'schema',
'createdAt',
'updatedAt',
]
]
]
],
]
);
}
3. Run the test and pass the test.
PS E:\wwwroot\object> ./vendor/bin/phpunit .\Modules\ThemeStore\Tests\Functional\GraphQl\OnlineStoreThemeGraphQlApiTest.php
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 996 ms, Memory: 60.00 MB
OK (1 test, 14 assertions)
PS E:\wwwroot\object>
