Skip to content

goca mocks

Generate testify/mock mocks for all interfaces of a named entity. Mocks let you unit-test use cases and handlers without hitting a real database or HTTP server.

Syntax

bash
goca mocks [EntityName] [flags]

Description

goca mocks scans the interfaces for the given entity (repository, use case, handler) and generates mock implementations using github.com/stretchr/testify/mock. When no entity name is provided it generates mocks for all entities found under internal/.

Mocks are written to internal/mocks/ and follow the naming convention mock_<entity>_<layer>.go.

Flags

--dry-run

Preview the files that would be created without writing anything to disk.

bash
goca mocks Product --dry-run

--force

Overwrite existing mock files.

bash
goca mocks Product --force

--backup

Back up existing mock files to .goca-backup/ before overwriting.

bash
goca mocks Product --backup

Examples

Generate mocks for one entity

bash
goca mocks Product

Generate mocks for all entities

bash
goca mocks

Regenerate with backup

bash
goca mocks Product --force --backup

Generated Files

FileDescription
internal/mocks/mock_product_repository.goRepository interface mock
internal/mocks/mock_product_usecase.goUse case interface mock
internal/mocks/examples/product_mock_examples_test.goUsage examples

Generated Code Example

go
// Code generated by goca. DO NOT EDIT by hand.
package mocks

import (
    "github.com/stretchr/testify/mock"
    "github.com/sazardev/myapp/internal/domain"
)

type MockProductRepository struct {
    mock.Mock
}

func (m *MockProductRepository) GetByID(id uint) (*domain.Product, error) {
    args := m.Called(id)
    return args.Get(0).(*domain.Product), args.Error(1)
}

Released under the MIT License.