go-mockery-descriptor

module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: MIT

README

go-mockery-descriptor

Generates descriptor structs for interface methods + mock constructors compatible with mockery / testify/mock.

Go Reference Go Report Card

What it does

go-mockery-descriptor parses Go interface definitions and produces two main outputs:

  • Structured descriptors — rich, compile-time-safe data structures that describe every method of the interface in detail
    (method name, parameter names & types, return types, etc.)
  • Mock constructor — helper function that creates a pre-configured *mock.Mock instance compatible with
    github.com/stretchr/testify/mock (the same foundation used by mockery)

This tool is especially useful when you need more control, introspection or customization than what standard mock generators provide.

Installation

go install github.com/xgamtx/go-mockery-descriptor/cmd/go-mockery-descriptor@latest
From source
git clone https://github.com/xgamtx/go-mockery-descriptor.git
cd go-mockery-descriptor
go install ./cmd/go-mockery-descriptor

Quick start

Suppose you have the following interface:

package service

import "context"

type UserService interface {
    GetUser(ctx context.Context, id string) (*User, error)
    ListUsers(ctx context.Context, filter *UserFilter) ([]User, error)
    CreateUser(ctx context.Context, user *User) error
}

Generate descriptors and mock constructor go-mockery-descriptor with config:

constructor-name: "newMock{{ . }}"
package-name: "{{ . }}"
output: "{{ . }}.gen_test.go"
interfaces:
  - name: UserService
    rename-returns:
      GetUser.r0: User
      ListUsers.r0: Users

This will create files similar to:

// Code generated by go-mockery-descriptor v1.0.0. DO NOT EDIT.

package service

import (
  "testing"

  "github.com/stretchr/testify/mock"
)

type getUserCall struct {
  Id           string
  ReceivedUser *User
  ReceivedErr  error
}

type listUsersCall struct {
  Filter        *UserFilter
  ReceivedUsers []User
  ReceivedErr   error
}

type createUserCall struct {
  User        *User
  ReceivedErr error
}

type userServiceCalls struct {
  GetUser    []getUserCall
  ListUsers  []listUsersCall
  CreateUser []createUserCall
}

func makeUserServiceMock(t *testing.T, calls *userServiceCalls) UserService {
  t.Helper()
  m := newMockUserService(t)
  anyCtx := mock.Anything
  for _, call := range calls.GetUser {
    m.EXPECT().GetUser(anyCtx, call.Id).Return(call.ReceivedUser, call.ReceivedErr).Once()
  }
  for _, call := range calls.ListUsers {
    m.EXPECT().ListUsers(anyCtx, call.Filter).Return(call.ReceivedUsers, call.ReceivedErr).Once()
  }
  for _, call := range calls.CreateUser {
    m.EXPECT().CreateUser(anyCtx, call.User).Return(call.ReceivedErr).Once()
  }

  return m
}

Why not just use mockery?

mockery is excellent when you want ready-to-use mock structs quickly.
go-mockery-descriptor gives you an intermediate structured representation — so you can describe method calls easily.

Status

🔧 Early development / proof-of-concept stage
CLI flags, output format and API are subject to change.

Feedback, use-cases and contributions are very welcome!

Contributing

Pull requests are welcome.
For major changes, please open an issue first to discuss what you would like to change.

License

MIT License

Directories

Path Synopsis
cmd
internal
app
pkg

Jump to

Keyboard shortcuts

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