mocks

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2025 License: MIT Imports: 5 Imported by: 0

README

Application User Mocks

This directory contains generated mocks and test utilities for the application user service interface.

Generated Mocks

  • mock_service.go - Mock implementation of user.Service interface

Test Utilities

  • test_utils.go - Helper functions and builders for common test scenarios
  • mock_generation_test.go - Tests to verify mock generation is working correctly

Usage

Basic Mock Usage
func TestSomething(t *testing.T) {
    ctrl := gomock.NewController(t)
    defer ctrl.Finish()
    
    mockService := mocks.NewMockService(ctrl)
    
    // Set up expectations
    req := user.GetUserRequest{ID: "test-id"}
    expectedResp := &user.GetUserResponse{
        User: &user.UserDTO{
            ID:    "test-id",
            Email: "test@example.com",
            Name:  "Test User",
        },
    }
    
    mockService.EXPECT().
        GetUser(gomock.Any(), req).
        Return(expectedResp, nil).
        Times(1)
    
    // Use the mock in your test
    result, err := mockService.GetUser(context.Background(), req)
    // ... assertions
}
Using Test Utilities
func TestWithUtilities(t *testing.T) {
    ctrl := gomock.NewController(t)
    defer ctrl.Finish()
    
    mockService := mocks.NewMockService(ctrl)
    utils := mocks.NewServiceTestUtils(mockService)
    
    // Use utility methods for common scenarios
    testUser := &user.UserDTO{
        ID:    "test-id",
        Email: "test@example.com",
        Name:  "Test User",
    }
    
    utils.ExpectGetUserSuccess("test-id", testUser)
    
    // Test your code
    req := user.GetUserRequest{ID: "test-id"}
    resp, err := mockService.GetUser(context.Background(), req)
    assert.NoError(t, err)
    assert.Equal(t, "test-id", resp.User.ID)
}
Building Test Data
func TestWithBuilder(t *testing.T) {
    // Create test DTOs with builder pattern
    userDTO := mocks.NewTestDTOBuilder().
        WithID("custom-id").
        WithEmail("custom@example.com").
        WithName("Custom User").
        BuildUserDTO()
    
    // Create user connections for pagination tests
    users := []*user.UserDTO{userDTO}
    connection := mocks.BuildTestUserConnection(users, true, false)
    
    // Use in your tests
    assert.Equal(t, 1, len(connection.Edges))
    assert.True(t, connection.PageInfo.HasNextPage)
}

Regenerating Mocks

To regenerate mocks after interface changes:

# Regenerate all mocks in this package
go generate ./internal/application/user/...

# Or regenerate all mocks in the project
go generate ./...

Available Test Utilities

ServiceTestUtils
  • ExpectGetUserSuccess(userID, returnUser) - Mock successful user retrieval
  • ExpectGetUserNotFound(userID) - Mock user not found error
  • ExpectListUsersSuccess(first, after, returnUsers) - Mock successful user listing
  • ExpectCreateUserSuccess(email, name, returnUser) - Mock successful user creation
  • ExpectCreateUserDuplicateEmail(email, name) - Mock duplicate email error
  • ExpectUpdateUserSuccess(userID, email, name, returnUser) - Mock successful user update
  • ExpectUpdateUserNotFound(userID, email, name) - Mock user not found for update
  • ExpectDeleteUserSuccess(userID) - Mock successful user deletion
  • ExpectDeleteUserNotFound(userID) - Mock user not found for deletion
TestDTOBuilder
  • WithID(id) - Set user ID
  • WithEmail(email) - Set user email
  • WithName(name) - Set user name
  • WithCreatedAt(time) - Set creation timestamp
  • WithUpdatedAt(time) - Set update timestamp
  • BuildUserDTO() - Create UserDTO instance
  • BuildUserEdgeDTO() - Create UserEdgeDTO instance
Helper Functions
  • BuildTestUserConnection(users, hasNextPage, hasPreviousPage) - Create test user connection for pagination
  • CommonTestContext() - Get context with reasonable timeout for tests

Testing Patterns

Testing GraphQL Resolvers
func TestUserResolver(t *testing.T) {
    ctrl := gomock.NewController(t)
    defer ctrl.Finish()
    
    mockService := mocks.NewMockService(ctrl)
    utils := mocks.NewServiceTestUtils(mockService)
    
    // Set up expectations
    testUser := mocks.NewTestDTOBuilder().
        WithID("test-id").
        BuildUserDTO()
    
    utils.ExpectGetUserSuccess("test-id", testUser)
    
    // Test resolver
    resolver := &Resolver{userService: mockService}
    result, err := resolver.User(context.Background(), "test-id")
    
    assert.NoError(t, err)
    assert.Equal(t, "test-id", result.ID)
}
Testing Pagination
func TestPagination(t *testing.T) {
    ctrl := gomock.NewController(t)
    defer ctrl.Finish()
    
    mockService := mocks.NewMockService(ctrl)
    utils := mocks.NewServiceTestUtils(mockService)
    
    // Create test data
    users := []*user.UserDTO{
        mocks.NewTestDTOBuilder().WithID("1").BuildUserDTO(),
        mocks.NewTestDTOBuilder().WithID("2").BuildUserDTO(),
    }
    
    connection := mocks.BuildTestUserConnection(users, true, false)
    utils.ExpectListUsersSuccess(10, "", connection)
    
    // Test your pagination logic
    // ...
}

Documentation

Overview

Package mocks is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildTestUserConnection

func BuildTestUserConnection(users []*user.UserDTO, hasNextPage, hasPreviousPage bool) *user.UserConnectionDTO

BuildTestUserConnection creates a test user connection with the given users

func CommonTestContext

func CommonTestContext() context.Context

CommonTestContext provides a common test context with timeout

Types

type MockService

type MockService struct {
	// contains filtered or unexported fields
}

MockService is a mock of Service interface.

func NewMockService

func NewMockService(ctrl *gomock.Controller) *MockService

NewMockService creates a new mock instance.

func (*MockService) CreateUser

CreateUser mocks base method.

func (*MockService) DeleteUser

DeleteUser mocks base method.

func (*MockService) EXPECT

func (m *MockService) EXPECT() *MockServiceMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockService) GetUser

GetUser mocks base method.

func (*MockService) ListUsers

ListUsers mocks base method.

func (*MockService) UpdateUser

UpdateUser mocks base method.

type MockServiceMockRecorder

type MockServiceMockRecorder struct {
	// contains filtered or unexported fields
}

MockServiceMockRecorder is the mock recorder for MockService.

func (*MockServiceMockRecorder) CreateUser

func (mr *MockServiceMockRecorder) CreateUser(ctx, req interface{}) *gomock.Call

CreateUser indicates an expected call of CreateUser.

func (*MockServiceMockRecorder) DeleteUser

func (mr *MockServiceMockRecorder) DeleteUser(ctx, req interface{}) *gomock.Call

DeleteUser indicates an expected call of DeleteUser.

func (*MockServiceMockRecorder) GetUser

func (mr *MockServiceMockRecorder) GetUser(ctx, req interface{}) *gomock.Call

GetUser indicates an expected call of GetUser.

func (*MockServiceMockRecorder) ListUsers

func (mr *MockServiceMockRecorder) ListUsers(ctx, req interface{}) *gomock.Call

ListUsers indicates an expected call of ListUsers.

func (*MockServiceMockRecorder) UpdateUser

func (mr *MockServiceMockRecorder) UpdateUser(ctx, req interface{}) *gomock.Call

UpdateUser indicates an expected call of UpdateUser.

type ServiceTestUtils

type ServiceTestUtils struct {
	// contains filtered or unexported fields
}

ServiceTestUtils provides common mock service setup utilities

func NewServiceTestUtils

func NewServiceTestUtils(mockService *MockService) *ServiceTestUtils

NewServiceTestUtils creates a new service test utilities instance

func (*ServiceTestUtils) ExpectCreateUserDuplicateEmail

func (u *ServiceTestUtils) ExpectCreateUserDuplicateEmail(email, name string)

ExpectCreateUserDuplicateEmail sets up mock to return duplicate email error

func (*ServiceTestUtils) ExpectCreateUserSuccess

func (u *ServiceTestUtils) ExpectCreateUserSuccess(email, name string, returnUser *user.UserDTO)

ExpectCreateUserSuccess sets up mock to create user successfully

func (*ServiceTestUtils) ExpectDeleteUserNotFound

func (u *ServiceTestUtils) ExpectDeleteUserNotFound(userID string)

ExpectDeleteUserNotFound sets up mock to return user not found error for deletion

func (*ServiceTestUtils) ExpectDeleteUserSuccess

func (u *ServiceTestUtils) ExpectDeleteUserSuccess(userID string)

ExpectDeleteUserSuccess sets up mock to delete user successfully

func (*ServiceTestUtils) ExpectGetUserNotFound

func (u *ServiceTestUtils) ExpectGetUserNotFound(userID string)

ExpectGetUserNotFound sets up mock to return user not found error

func (*ServiceTestUtils) ExpectGetUserSuccess

func (u *ServiceTestUtils) ExpectGetUserSuccess(userID string, returnUser *user.UserDTO)

ExpectGetUserSuccess sets up mock to return a user successfully

func (*ServiceTestUtils) ExpectListUsersSuccess

func (u *ServiceTestUtils) ExpectListUsersSuccess(first int, after string, returnUsers *user.UserConnectionDTO)

ExpectListUsersSuccess sets up mock to return users list successfully

func (*ServiceTestUtils) ExpectUpdateUserNotFound

func (u *ServiceTestUtils) ExpectUpdateUserNotFound(userID string, email, name *string)

ExpectUpdateUserNotFound sets up mock to return user not found error for update

func (*ServiceTestUtils) ExpectUpdateUserSuccess

func (u *ServiceTestUtils) ExpectUpdateUserSuccess(userID string, email, name *string, returnUser *user.UserDTO)

ExpectUpdateUserSuccess sets up mock to update user successfully

type TestDTOBuilder

type TestDTOBuilder struct {
	// contains filtered or unexported fields
}

TestDTOBuilder helps create test DTOs with common configurations

func NewTestDTOBuilder

func NewTestDTOBuilder() *TestDTOBuilder

NewTestDTOBuilder creates a new test DTO builder with default values

func (*TestDTOBuilder) BuildUserDTO

func (b *TestDTOBuilder) BuildUserDTO() *user.UserDTO

BuildUserDTO creates a UserDTO with the configured values

func (*TestDTOBuilder) BuildUserEdgeDTO

func (b *TestDTOBuilder) BuildUserEdgeDTO() *user.UserEdgeDTO

BuildUserEdgeDTO creates a UserEdgeDTO with the configured values

func (*TestDTOBuilder) WithCreatedAt

func (b *TestDTOBuilder) WithCreatedAt(createdAt time.Time) *TestDTOBuilder

WithCreatedAt sets the created at timestamp

func (*TestDTOBuilder) WithEmail

func (b *TestDTOBuilder) WithEmail(email string) *TestDTOBuilder

WithEmail sets the user email

func (*TestDTOBuilder) WithID

func (b *TestDTOBuilder) WithID(id string) *TestDTOBuilder

WithID sets the user ID

func (*TestDTOBuilder) WithName

func (b *TestDTOBuilder) WithName(name string) *TestDTOBuilder

WithName sets the user name

func (*TestDTOBuilder) WithUpdatedAt

func (b *TestDTOBuilder) WithUpdatedAt(updatedAt time.Time) *TestDTOBuilder

WithUpdatedAt sets the updated at timestamp

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL