首页 > 后端开发 > Golang > 正文

如何使用 gqgen 在 Go 中对 GraphQL 查询和突变进行单元测试?

Mary-Kate Olsen
发布: 2024-10-30 08:52:02
原创
988 人浏览过

How to Unit Test GraphQL Queries and Mutations in Go with gqgen?

Go 中的 GraphQL 单元测试

使用 GraphQL 和 Go 时,单元测试对于确保代码的正确性和可靠性至关重要。这是一个简单的示例,演示如何使用 Go 有效地对 GraphQL 查询和突变进行单元测试,同时考虑到您提到的框架:

<code class="go">package resolver_test

import (
    "testing"
    "github.com/99designs/gqlgen/client"
    "github.com/99designs/gqlgen/graphql/handler"
    "github.com/mrdulin/gqlgen-cnode/graph/generated"
    "github.com/mrdulin/gqlgen-cnode/graph/model"
    "github.com/mrdulin/gqlgen-cnode/graph/resolver"
    "github.com/mrdulin/gqlgen-cnode/mocks"
    "github.com/stretchr/testify/mock"
    "github.com/stretchr/testify/require"
)

// Define mock objects for UserService
type MockedUserService struct {
    mock.Mock
}

func (s *MockedUserService) GetUserByLoginname(loginname string) *model.UserDetail {
    args := s.Called(loginname)
    return args.Get(0).(*model.UserDetail)
}

func (s *MockedUserService) ValidateAccessToken(accesstoken string) *model.UserEntity {
    args := s.Called(accesstoken)
    return args.Get(0).(*model.UserEntity)
}

// Test MutationResolver_ValidateAccessToken
func TestMutationResolver_ValidateAccessToken(t *testing.T) {

    t.Run("should validate accesstoken correctly", func(t *testing.T) {
        // Create a mock UserService
        testUserService := new(mocks.MockedUserService)
        resolvers := resolver.Resolver{UserService: testUserService}

        // Create a GraphQL client
        c := client.New(handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &resolvers})))

        // Define the expected user entity
        ue := model.UserEntity{ID: "123", User: model.User{Loginname: &loginname, AvatarURL: &avatarURL}}

        // Set the mock UserService to return the expected user entity
        testUserService.On("ValidateAccessToken", "abc").Return(&ue)

        // Define the query to test
        var resp struct {
            ValidateAccessToken struct{ ID, Loginname, AvatarUrl string }
        }
        q := `
      mutation { 
        validateAccessToken(accesstoken: "abc") { 
          id, 
          loginname, 
          avatarUrl 
        } 
      }
    `

        // Execute the query and assert the results
        c.MustPost(q, &resp)
        testUserService.AssertExpectations(t)
        require.Equal(t, "123", resp.ValidateAccessToken.ID)
        require.Equal(t, loginname, resp.ValidateAccessToken.Loginname)
        require.Equal(t, avatarURL, resp.ValidateAccessToken.AvatarUrl)
    })
}

// Test QueryResolver_User
func TestQueryResolver_User(t *testing.T) {

    t.Run("should query user correctly", func(t *testing.T) {
        // Create a mock UserService
        testUserService := new(mocks.MockedUserService)
        resolvers := resolver.Resolver{UserService: testUserService}

        // Create a GraphQL client
        c := client.New(handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &resolvers})))

        // Define the expected user detail
        u := model.UserDetail{User: model.User{Loginname: &loginname, AvatarURL: &avatarURL}, Score: &score, CreateAt: &createAt}
</code>
登录后复制

以上是如何使用 gqgen 在 Go 中对 GraphQL 查询和突变进行单元测试?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!