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 --backupExamples
Generate mocks for one entity
bash
goca mocks ProductGenerate mocks for all entities
bash
goca mocksRegenerate with backup
bash
goca mocks Product --force --backupGenerated Files
| File | Description |
|---|---|
internal/mocks/mock_product_repository.go | Repository interface mock |
internal/mocks/mock_product_usecase.go | Use case interface mock |
internal/mocks/examples/product_mock_examples_test.go | Usage 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)
}Related Commands
goca feature— generate all layers including mocks via--mocksflaggoca interfaces— generate the interface contracts that mocks implementgoca test-integration— generate integration test scaffolding