首頁 > 後端開發 > Golang > 主體

如何使用 gqgen 在 Go 中對 GraphQL 查詢和突變進行單元測試?

Mary-Kate Olsen
發布: 2024-10-30 08:52:02
原創
989 人瀏覽過

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學習者快速成長!