编写 Lighthouse 5 的自动化测试用例时,断言响应具有给定的 JSON 结构,即仅验证字段是否存在,不验证其值
1、运行 GraphQL Query API,响应 200。主要测试字段:themeAssets 的响应。如图1
2、添加测试字段:themeAssets 。主要验证字段是否存在。因为字段的值无法预测。也不准备插入一条新记录。断言响应具有给定的 JSON 结构,基于 $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、运行测试,测试通过。
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>
近期评论