简体   繁体   English

如何使用Laravel为Graphql应用程序创建单元测试?

[英]How can i created unit testing to Graphql application with Laravel?

I created an application with Laravel using Graphql. 我使用Graphql用Laravel创建了一个应用程序。 But I dont how to create my unit tests. 但是我不怎么创建我的单元测试。 Only do it simple request with PHPUnit or have other better way to do it? 仅使用PHPUnit进行简单请求还是有其他更好的方法?

Example Query Graphql in Laravel: Laravel中的示例查询Graphql:

class ClientQuery extends Query
{
    protected $attributes = [
        'name' => 'ClientQuery',
        'description' => 'A query'
    ];

    public function type()
    {
       return GraphQL::type("Client");
    }

    public function args()
    {
       return [
            'id' => [
                 'type' => Type::nonNull(Type::int()),
            ],
       ];
    }

    public function resolve($root, $args, $context, ResolveInfo $info)
    {
      return Client::find($args['id']);
    }
}

I'm currently using GraphQL like you. 我目前正在像您一样使用GraphQL。 Because I've used GraphQL files to describe my data model, I have only my resolver in PHP (like you no?). 因为我使用GraphQL文件描述了我的数据模型,所以我只有PHP的解析器(就像您一样吗?)。

I think you can create Unit test but it is not necessary because is the role of data model to avoid errors of Type. 我认为您可以创建单元测试,但这不是必需的,因为数据模型的作用是避免类型错误。 But you can make it to perform a better product/platform yes. 但是,您可以使它执行更好的产品/平台。

In this way, Integration tests are very important, to test resolvers. 这样,集成测试对于测试解析器非常重要。

I'm currently using API call to test resolvers, but it can change because I'm not an expert and I don't think it's the better way but for now that make the job. 我目前正在使用API​​调用来测试解析器,但是它可以更改,因为我不是专家,我认为这不是更好的方法,但目前可以成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM