在 Lighthouse 5 中,基于 PHPUnit 编写 Mutation 的测试

1、在 Altair GraphQl Client 中运行成功。如图1

在 Altair GraphQl Client 中运行成功。

图1

mutation CreateThemeAsset {
  onlineStoreThemeAssetCreate(
    input: { themeId: "vogue", content: "string", key: "js/20220307.4.js" }
  ) {
    themeAsset {
      themeId
      key
      content
    }
  }
}


{
  "data": {
    "onlineStoreThemeAssetCreate": {
      "themeAsset": {
        "themeId": "vogue",
        "key": "js/20220307.4.js",
        "content": "string"
      }
    }
  }
}

2、编辑测试文件,/Modules/ThemeStore/Tests/Functional/GraphQl/MutationThemeAssetGraphQlApiTest.php。

<?php

namespace Modules/ThemeStore/Tests/Functional/GraphQl;

use Nuwave/Lighthouse/Testing/MakesGraphQLRequests;
use Illuminate/Foundation/Testing/TestCase as BaseTestCase;
use Nuwave/Lighthouse/Testing/ClearsSchemaCache;
use Sentry/Util/JSON;
use Tests/CreatesApplication;

class MutationThemeAssetGraphQlApiTest extends BaseTestCase
{
    use CreatesApplication,
        ClearsSchemaCache,
        MakesGraphQLRequests;

    public function testCreateThemeAsset(): void
    {

        $input = [
            'themeId' => 'vogue',
            'content' => 'string',
            'key' => 'js/20220307.8.js',
        ];

        $response = $this->graphQL('
            mutation CreateThemeAsset($input: ThemeAssetCreateInput!) {
                onlineStoreThemeAssetCreate(input: $input) {
                    themeAsset {
                        themeId
                        key
                        content
                    }
                }
            }
        ', [
            'input' => json_encode($input)
        ]);

        $response->assertJson(
            [
                'data' => [
                    'onlineStoreThemeAssetCreate' => [
                        'themeAsset' => $input
                    ],
                ],
            ]
        );
    }

    protected function setUp(): void
    {
        parent::setUp();

        $this->bootClearsSchemaCache();
    }
}



3、运行测试用例,报错:Expected type ThemeAssetCreateInput to be an object.。如图2

运行测试用例,报错:Expected type ThemeAssetCreateInput to be an object.。

图2

PS E:/wwwroot/wshop/platform> ./vendor/bin/phpunit --process-isolation ./Modules/ThemeStore/Tests/Functional/GraphQl/MutationThemeAssetGraphQlApiTest.php
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 2.89 seconds, Memory: 16.00 MB

There was 1 failure:

1) Modules/ThemeStore/Tests/Functional/GraphQl/MutationThemeAssetGraphQlApiTest::testCreateThemeAsset
Unable to find JSON:

[{
    "data": {
        "onlineStoreThemeAssetCreate": {
            "themeAsset": {
                "themeId": "vogue",
                "content": "string",
                "key": "js/20220307.8.js"
            }
        }
    }
}]

within response JSON:

[{
    "errors": [
        {
            "message": "Variable /"$input/" got invalid value /"{/"themeId/":/"vogue/",/"content/":/"string/",/"key/":/"js///20220307.8.js/"}/"; Expected type ThemeAssetCreateInput to be an object.",
            "extensions": {
                "category": "graphql"
            },
            "locations": [
                {
                    "line": 2,
                    "column": 39
                }
            ]
        }
    ]
}].


Failed asserting that an array has the subset Array &0 (
    'data' => Array &1 (
        'onlineStoreThemeAssetCreate' => Array &2 (
            'themeAsset' => Array &3 (
                'themeId' => 'vogue'
                'content' => 'string'
                'key' => 'js/20220307.8.js'
            )
        )
    )
).
--- Expected
+++ Actual
@@ @@
       ),
     ),
   ),
-  'data' =>
-  array (
-    'onlineStoreThemeAssetCreate' =>
-    array (
-      'themeAsset' =>
-      array (
-        'themeId' => 'vogue',
-        'content' => 'string',
-        'key' => 'js/20220307.8.js',
-      ),
-    ),
-  ),
 )

E:/wwwroot/wshop/platform/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Assert.php:108
E:/wwwroot/wshop/platform/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:479
E:/wwwroot/wshop/platform/Modules/ThemeStore/Tests/Functional/GraphQl/MutationThemeAssetGraphQlApiTest.php:56

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
PS E:/wwwroot/wshop/platform>

4、重新编辑测试文件

<?php

namespace Modules/ThemeStore/Tests/Functional/GraphQl;

use Nuwave/Lighthouse/Testing/MakesGraphQLRequests;
use Illuminate/Foundation/Testing/TestCase as BaseTestCase;
use Nuwave/Lighthouse/Testing/ClearsSchemaCache;
use Sentry/Util/JSON;
use Tests/CreatesApplication;

class MutationThemeAssetGraphQlApiTest extends BaseTestCase
{
    use CreatesApplication,
        ClearsSchemaCache,
        MakesGraphQLRequests;

    public function testCreateThemeAsset(): void
    {
        $input = [
            'themeId' => 'vogue',
            'content' => 'string',
            'key' => 'js/20220307.9.js',
        ];

        $response = $this->graphQL('
            mutation CreateThemeAsset($input: ThemeAssetCreateInput!) {
                onlineStoreThemeAssetCreate(input: $input) {
                    themeAsset {
                        themeId
                        key
                        content
                    }
                }
            }
        ', [
            'input' => json_decode(json_encode($input))
        ]);

        $response->assertJson(
            [
                'data' => [
                    'onlineStoreThemeAssetCreate' => [
                        'themeAsset' => $input
                    ],
                ],
            ]
        );
    }

    protected function setUp(): void
    {
        parent::setUp();

        $this->bootClearsSchemaCache();
    }
}



5、再次运行测试,测试通过。

PS E:/wwwroot/wshop/platform> ./vendor/bin/phpunit --process-isolation ./Modules/ThemeStore/Tests/Functional/GraphQl/MutationThemeAssetGraphQlApiTest.php
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 2.61 seconds, Memory: 16.00 MB

OK (1 test, 1 assertion)

6、再运行一次测试,测试未通过。原因在于同样的请求参数,不允许重复,否则验证失败,进而测试未通过断言。

PS E:/wwwroot/wshop/platform> ./vendor/bin/phpunit --process-isolation ./Modules/ThemeStore/Tests/Functional/GraphQl/MutationThemeAssetGraphQlApiTest.php
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
F                                                                   1 / 1 (100%)
Time: 2.77 seconds, Memory: 18.00 MB
There was 1 failure:
1) Modules/ThemeStore/Tests/Functional/GraphQl/MutationThemeAssetGraphQlApiTest::testCreateThemeAsset
Unable to find JSON:
[{
"data": {
"onlineStoreThemeAssetCreate": {
"themeAsset": {
"themeId": "vogue",
"content": "string",
"key": "js/20220307.9.js"
}
}
}
}]
within response JSON:
[{
"errors": [
{
"message": "Validation failed for the field [onlineStoreThemeAssetCreate].",
"extensions": {
"validation": {
"input.key": [
"The input.key has already been taken"
]
},
"category": "validation"
},
"locations": [
{
"line": 3,
"column": 17
}
],
"path": [
"onlineStoreThemeAssetCreate"
],
"trace": [
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//Schema//Directives//ArgTraversalDirective.php",
"line": 29,
"call": "Nuwave//Lighthouse//Validation//ValidateDirective::Nuwave//Lighthouse//Validation//{closure}(null, array(1), instance of Nuwave//Lighthouse//Schema//Context, instance of GraphQL//Type//Definition//ResolveInfo)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//Schema//Directives//TrimDirective.php",
"line": 56,
"call": "Nuwave//Lighthouse//Schema//Directives//ArgTraversalDirective::Nuwave//Lighthouse//Schema//Directives//{closure}(null, array(1), instance of Nuwave//Lighthouse//Schema//Context, instance of GraphQL//Type//Definition//ResolveInfo)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//Schema//Factories//FieldFactory.php",
"line": 99,
"call": "Nuwave//Lighthouse//Schema//Directives//TrimDirective::Nuwave//Lighthouse//Schema//Directives//{closure}(null, array(1), instance of Nuwave//Lighthouse//Schema//Context, instance of GraphQL//Type//Definition//ResolveInfo)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//webonyx//graphql-php//src//Executor//ReferenceExecutor.php",
"line": 623,
"call": "Nuwave//Lighthouse//Schema//Factories//FieldFactory::Nuwave//Lighthouse//Schema//Factories//{closure}(null, array(1), instance of Nuwave//Lighthouse//Schema//Context, instance of GraphQL//Type//Definition//ResolveInfo)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//webonyx//graphql-php//src//Executor//ReferenceExecutor.php",
"line": 550,
"call": "GraphQL//Executor//ReferenceExecutor::resolveFieldValueOrError(instance of GraphQL//Type//Definition//FieldDefinition, instance of GraphQL//Language//AST//FieldNode, instance of Closure, null, instance of GraphQL//Type//Definition//ResolveInfo)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//webonyx//graphql-php//src//Executor//ReferenceExecutor.php",
"line": 474,
"call": "GraphQL//Executor//ReferenceExecutor::resolveField(GraphQLType: Mutation, null, instance of ArrayObject(1), array(1))"
},
{
"file": "E://wwwroot//wshop//platform//vendor//webonyx//graphql-php//src//Executor//ReferenceExecutor.php",
"line": 857,
"call": "GraphQL//Executor//ReferenceExecutor::GraphQL//Executor//{closure}(array(0), 'onlineStoreThemeAssetCreate')"
},
{
"call": "GraphQL//Executor//ReferenceExecutor::GraphQL//Executor//{closure}(array(0), 'onlineStoreThemeAssetCreate')"
},
{
"file": "E://wwwroot//wshop//platform//vendor//webonyx//graphql-php//src//Executor//ReferenceExecutor.php",
"line": 859,
"function": "array_reduce(array(1), instance of Closure, array(0))"
},
{
"file": "E://wwwroot//wshop//platform//vendor//webonyx//graphql-php//src//Executor//ReferenceExecutor.php",
"line": 490,
"call": "GraphQL//Executor//ReferenceExecutor::promiseReduce(array(1), instance of Closure, array(0))"
},
{
"file": "E://wwwroot//wshop//platform//vendor//webonyx//graphql-php//src//Executor//ReferenceExecutor.php",
"line": 263,
"call": "GraphQL//Executor//ReferenceExecutor::executeFieldsSerially(GraphQLType: Mutation, null, array(0), instance of ArrayObject(1))"
},
{
"file": "E://wwwroot//wshop//platform//vendor//webonyx//graphql-php//src//Executor//ReferenceExecutor.php",
"line": 215,
"call": "GraphQL//Executor//ReferenceExecutor::executeOperation(instance of GraphQL//Language//AST//OperationDefinitionNode, null)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//webonyx//graphql-php//src//Executor//Executor.php",
"line": 156,
"call": "GraphQL//Executor//ReferenceExecutor::doExecute()"
},
{
"file": "E://wwwroot//wshop//platform//vendor//webonyx//graphql-php//src//GraphQL.php",
"line": 162,
"call": "GraphQL//Executor//Executor::promiseToExecute(instance of GraphQL//Executor//Promise//Adapter//SyncPromiseAdapter, instance of GraphQL//Type//Schema, instance of GraphQL//Language//AST//DocumentNode, null, instance of Nuwave//Lighthouse//Schema//Context, array(1), null, null)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//webonyx//graphql-php//src//GraphQL.php",
"line": 94,
"call": "GraphQL//GraphQL::promiseToExecute(instance of GraphQL//Executor//Promise//Adapter//SyncPromiseAdapter, instance of GraphQL//Type//Schema, instance of GraphQL//Language//AST//DocumentNode, null, instance of Nuwave//Lighthouse//Schema//Context, array(1), null, null, array(29))"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//GraphQL.php",
"line": 268,
"call": "GraphQL//GraphQL::executeQuery(instance of GraphQL//Type//Schema, instance of GraphQL//Language//AST//DocumentNode, null, instance of Nuwave//Lighthouse//Schema//Context, array(1), null, null, array(29))"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//GraphQL.php",
"line": 203,
"call": "Nuwave//Lighthouse//GraphQL::executeParsedQuery(instance of GraphQL//Language//AST//DocumentNode, instance of Nuwave//Lighthouse//Schema//Context, array(1), null, null)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//GraphQL.php",
"line": 162,
"call": "Nuwave//Lighthouse//GraphQL::parseAndExecuteQuery('/n            mutation CreateThemeAsset($input: ThemeAssetCreateInput!) {/n                onlineStoreThemeAssetCreate(input: $input) {/n                    themeAsset {/n                        themeId/n                        key/n                        content/n                    }/n                }/n            }/n        ', instance of Nuwave//Lighthouse//Schema//Context, array(1), null, null)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//GraphQL.php",
"line": 121,
"call": "Nuwave//Lighthouse//GraphQL::executeOperation(instance of GraphQL//Server//OperationParams, instance of Nuwave//Lighthouse//Schema//Context)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//Support//Utils.php",
"line": 99,
"call": "Nuwave//Lighthouse//GraphQL::Nuwave//Lighthouse//{closure}(instance of GraphQL//Server//OperationParams)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//GraphQL.php",
"line": 120,
"call": "Nuwave//Lighthouse//Support//Utils::applyEach(instance of Closure, instance of GraphQL//Server//OperationParams)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//Support//Http//Controllers//GraphQLController.php",
"line": 32,
"call": "Nuwave//Lighthouse//GraphQL::executeOperationOrOperations(instance of GraphQL//Server//OperationParams, instance of Nuwave//Lighthouse//Schema//Context)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Routing//ControllerDispatcher.php",
"line": 48,
"call": "Nuwave//Lighthouse//Support//Http//Controllers//GraphQLController::__invoke(instance of Illuminate//Http//Request, instance of Nuwave//Lighthouse//GraphQL, instance of Illuminate//Events//Dispatcher, instance of Laragraph//Utils//RequestParser, instance of Nuwave//Lighthouse//Execution//SingleResponse, instance of Nuwave//Lighthouse//Execution//ContextFactory)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Routing//Route.php",
"line": 219,
"call": "Illuminate//Routing//ControllerDispatcher::dispatch(instance of Illuminate//Routing//Route, instance of Nuwave//Lighthouse//Support//Http//Controllers//GraphQLController, '__invoke')"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Routing//Route.php",
"line": 176,
"call": "Illuminate//Routing//Route::runController()"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Routing//Router.php",
"line": 681,
"call": "Illuminate//Routing//Route::run()"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 130,
"call": "Illuminate//Routing//Router::Illuminate//Routing//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//Support//Http//Middleware//AttemptAuthentication.php",
"line": 34,
"call": "Illuminate//Pipeline//Pipeline::Illuminate//Pipeline//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 171,
"call": "Nuwave//Lighthouse//Support//Http//Middleware//AttemptAuthentication::handle(instance of Illuminate//Http//Request, instance of Closure)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//Support//Http//Middleware//AcceptJson.php",
"line": 27,
"call": "Illuminate//Pipeline//Pipeline::Illuminate//Pipeline//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 171,
"call": "Nuwave//Lighthouse//Support//Http//Middleware//AcceptJson::handle(instance of Illuminate//Http//Request, instance of Closure)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 105,
"call": "Illuminate//Pipeline//Pipeline::Illuminate//Pipeline//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Routing//Router.php",
"line": 683,
"call": "Illuminate//Pipeline//Pipeline::then(instance of Closure)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Routing//Router.php",
"line": 658,
"call": "Illuminate//Routing//Router::runRouteWithinStack(instance of Illuminate//Routing//Route, instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Routing//Router.php",
"line": 624,
"call": "Illuminate//Routing//Router::runRoute(instance of Illuminate//Http//Request, instance of Illuminate//Routing//Route)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Routing//Router.php",
"line": 613,
"call": "Illuminate//Routing//Router::dispatchToRoute(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Foundation//Http//Kernel.php",
"line": 170,
"call": "Illuminate//Routing//Router::dispatch(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 130,
"call": "Illuminate//Foundation//Http//Kernel::Illuminate//Foundation//Http//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//barryvdh//laravel-debugbar//src//Middleware//InjectDebugbar.php",
"line": 60,
"call": "Illuminate//Pipeline//Pipeline::Illuminate//Pipeline//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 171,
"call": "Barryvdh//Debugbar//Middleware//InjectDebugbar::handle(instance of Illuminate//Http//Request, instance of Closure)"
},
{
"file": "E://wwwroot//wshop//platform//app//Http//Middleware//ChangeAppUrlMiddleware.php",
"line": 23,
"call": "Illuminate//Pipeline//Pipeline::Illuminate//Pipeline//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 171,
"call": "App//Http//Middleware//ChangeAppUrlMiddleware::handle(instance of Illuminate//Http//Request, instance of Closure)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Foundation//Http//Middleware//TransformsRequest.php",
"line": 21,
"call": "Illuminate//Pipeline//Pipeline::Illuminate//Pipeline//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 171,
"call": "Illuminate//Foundation//Http//Middleware//TransformsRequest::handle(instance of Illuminate//Http//Request, instance of Closure)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Foundation//Http//Middleware//TransformsRequest.php",
"line": 21,
"call": "Illuminate//Pipeline//Pipeline::Illuminate//Pipeline//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 171,
"call": "Illuminate//Foundation//Http//Middleware//TransformsRequest::handle(instance of Illuminate//Http//Request, instance of Closure)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Foundation//Http//Middleware//ValidatePostSize.php",
"line": 27,
"call": "Illuminate//Pipeline//Pipeline::Illuminate//Pipeline//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 171,
"call": "Illuminate//Foundation//Http//Middleware//ValidatePostSize::handle(instance of Illuminate//Http//Request, instance of Closure)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Foundation//Http//Middleware//CheckForMaintenanceMode.php",
"line": 63,
"call": "Illuminate//Pipeline//Pipeline::Illuminate//Pipeline//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 171,
"call": "Illuminate//Foundation//Http//Middleware//CheckForMaintenanceMode::handle(instance of Illuminate//Http//Request, instance of Closure)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//fideloper//proxy//src//TrustProxies.php",
"line": 57,
"call": "Illuminate//Pipeline//Pipeline::Illuminate//Pipeline//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 171,
"call": "Fideloper//Proxy//TrustProxies::handle(instance of Illuminate//Http//Request, instance of Closure)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//dingo//api//src//Http//Middleware//Request.php",
"line": 111,
"call": "Illuminate//Pipeline//Pipeline::Illuminate//Pipeline//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 171,
"call": "Dingo//Api//Http//Middleware//Request::handle(instance of Illuminate//Http//Request, instance of Closure)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Pipeline//Pipeline.php",
"line": 105,
"call": "Illuminate//Pipeline//Pipeline::Illuminate//Pipeline//{closure}(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Foundation//Http//Kernel.php",
"line": 145,
"call": "Illuminate//Pipeline//Pipeline::then(instance of Closure)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Foundation//Http//Kernel.php",
"line": 110,
"call": "Illuminate//Foundation//Http//Kernel::sendRequestThroughRouter(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Foundation//Testing//Concerns//MakesHttpRequests.php",
"line": 470,
"call": "Illuminate//Foundation//Http//Kernel::handle(instance of Illuminate//Http//Request)"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Foundation//Testing//Concerns//MakesHttpRequests.php",
"line": 442,
"call": "Illuminate//Foundation//Testing//TestCase::call('POST', 'https://wshop.local/graphql', array(0), array(0), array(1), array(3), '{/"query/":/"//n            mutation CreateThemeAsset($input: ThemeAssetCreateInput!) {//n                onlineStoreThemeAssetCreate(input: $input) {//n                    themeAsset {//n                        themeId//n                        key//n                        content//n                    }//n                }//n            }//n        /",/"variables/":{/"input/":{/"themeId/":/"vogue/",/"content/":/"string/",/"key/":/"js///20220307.9.js/"}}}')"
},
{
"file": "E://wwwroot//wshop//platform//vendor//laravel//framework//src//Illuminate//Foundation//Testing//Concerns//MakesHttpRequests.php",
"line": 301,
"call": "Illuminate//Foundation//Testing//TestCase::json('POST', 'https://wshop.local/graphql', array(2), array(3))"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//Testing//MakesGraphQLRequests.php",
"line": 79,
"call": "Illuminate//Foundation//Testing//TestCase::postJson('https://wshop.local/graphql', array(2), array(0))"
},
{
"file": "E://wwwroot//wshop//platform//vendor//nuwave//lighthouse//src//Testing//MakesGraphQLRequests.php",
"line": 62,
"call": "Modules//ThemeStore//Tests//Functional//GraphQl//MutationThemeAssetGraphQlApiTest::postGraphQL(array(2), array(0))"
},
{
"file": "E://wwwroot//wshop//platform//Modules//ThemeStore//Tests//Functional//GraphQl//MutationThemeAssetGraphQlApiTest.php",
"line": 36,
"call": "Modules//ThemeStore//Tests//Functional//GraphQl//MutationThemeAssetGraphQlApiTest::graphQL('/n            mutation CreateThemeAsset($input: ThemeAssetCreateInput!) {/n                onlineStoreThemeAssetCreate(input: $input) {/n                    themeAsset {/n                        themeId/n                        key/n                        content/n                    }/n                }/n            }/n        ', array(1))"
},
{
"file": "E://wwwroot//wshop//platform//vendor//phpunit//phpunit//src//Framework//TestCase.php",
"line": 1154,
"call": "Modules//ThemeStore//Tests//Functional//GraphQl//MutationThemeAssetGraphQlApiTest::testCreateThemeAsset()"
},
{
"file": "E://wwwroot//wshop//platform//vendor//phpunit//phpunit//src//Framework//TestCase.php",
"line": 842,
"call": "PHPUnit//Framework//TestCase::runTest()"
},
{
"file": "E://wwwroot//wshop//platform//vendor//phpunit//phpunit//src//Framework//TestResult.php",
"line": 693,
"call": "PHPUnit//Framework//TestCase::runBare()"
},
{
"file": "E://wwwroot//wshop//platform//vendor//phpunit//phpunit//src//Framework//TestCase.php",
"line": 796,
"call": "PHPUnit//Framework//TestResult::run(instance of Modules//ThemeStore//Tests//Functional//GraphQl//MutationThemeAssetGraphQlApiTest(1))"
},
{
"file": "C://Users//Lenovo//AppData//Local//Temp//PHP57E0.tmp",
"line": 261,
"call": "PHPUnit//Framework//TestCase::run(instance of PHPUnit//Framework//TestResult(1))"
},
{
"file": "C://Users//Lenovo//AppData//Local//Temp//PHP57E0.tmp",
"line": 1040,
"function": "__phpunit_run_isolated_test()"
}
]
}
],
"data": {
"onlineStoreThemeAssetCreate": null
}
}].
Failed asserting that an array has the subset Array &0 (
'data' => Array &1 (
'onlineStoreThemeAssetCreate' => Array &2 (
'themeAsset' => Array &3 (
'themeId' => 'vogue'
'content' => 'string'
'key' => 'js/20220307.9.js'
)
)
)
).
--- Expected
+++ Actual
@@ @@
),
'data' =>
array (
-    'onlineStoreThemeAssetCreate' =>
-    array (
-      'themeAsset' =>
-      array (
-        'themeId' => 'vogue',
-        'content' => 'string',
-        'key' => 'js/20220307.9.js',
-      ),
-    ),
+    'onlineStoreThemeAssetCreate' => NULL,
),
)
E:/wwwroot/wshop/platform/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Assert.php:108
E:/wwwroot/wshop/platform/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:479
E:/wwwroot/wshop/platform/Modules/ThemeStore/Tests/Functional/GraphQl/MutationThemeAssetGraphQlApiTest.php:43
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
PS E:/wwwroot/wshop/platform>

7、虽然现在针对测试用例配置了一个单独的测试数据库,无需要担心影响到开发数据库。但是要避免多次测试时,测试不通过的问题,决定尽量避免掉请求参数的重复。将变量 key 的值基于时间戳生成。其值示例:js/20220307144656.js。这样可以保证在每一秒钟可以运行一次测试,且能够通过。后续可以在此测试文件中添加一个测试删除此条创建的记录的方法。如图3

虽然现在针对测试用例配置了一个单独的测试数据库,无需要担心影响到开发数据库。但是要避免多次测试时,测试不通过的问题,决定尽量避免掉请求参数的重复。将变量 key 的值基于时间戳生成。其值示例:js/20220307144656.js。这样可以保证在每一秒钟可以运行一次测试,且能够通过。后续可以在此测试文件中添加一个测试删除此条创建的记录的方法。

图3

$input = [
'themeId' => 'vogue',
'content' => 'string',
'key' => 'js/' . date('YmdHis') . '.js',
];

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/250732.html

(0)
上一篇 2022年5月1日
下一篇 2022年5月1日

相关推荐

发表回复

登录后才能评论