{"id":20709624,"url":"https://github.com/go-andiamo/mmock","last_synced_at":"2026-06-01T00:32:15.792Z","repository":{"id":188017944,"uuid":"663177530","full_name":"go-andiamo/mmock","owner":"go-andiamo","description":"Add-on for testify mocks - allowing methods to be specified by func","archived":false,"fork":false,"pushed_at":"2023-08-26T09:07:40.000Z","size":21,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-17T21:06:43.204Z","etag":null,"topics":["golang","mock","mocking","testify","testify-mocking"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/go-andiamo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-07-06T18:16:55.000Z","updated_at":"2023-09-08T09:47:11.000Z","dependencies_parsed_at":"2023-08-13T10:21:25.344Z","dependency_job_id":"f2776b10-c2e6-4a85-a8ea-be21ef30b049","html_url":"https://github.com/go-andiamo/mmock","commit_stats":null,"previous_names":["go-andiamo/mmock"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-andiamo%2Fmmock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-andiamo%2Fmmock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-andiamo%2Fmmock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-andiamo%2Fmmock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-andiamo","download_url":"https://codeload.github.com/go-andiamo/mmock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242980784,"owners_count":20216285,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","mock","mocking","testify","testify-mocking"],"created_at":"2024-11-17T02:07:20.206Z","updated_at":"2025-12-12T11:35:05.394Z","avatar_url":"https://github.com/go-andiamo.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MMOCK (Method Mock)\n[![GoDoc](https://godoc.org/github.com/go-andiamo/mmock?status.svg)](https://pkg.go.dev/github.com/go-andiamo/mmock)\n[![Latest Version](https://img.shields.io/github/v/tag/go-andiamo/mmock.svg?sort=semver\u0026style=flat\u0026label=version\u0026color=blue)](https://github.com/go-andiamo/mmock/releases)\n[![Go Report Card](https://goreportcard.com/badge/github.com/go-andiamo/mmock)](https://goreportcard.com/report/github.com/go-andiamo/mmock)\n\nmmock (method mock) is an overlay for the popular https://pkg.go.dev/github.com/stretchr/testify/mock \nthat allows mocked methods to be specified by function rather than string name.\nThe problem with specifying methods by string name is that refactoring tools will miss these.\n\nAs per this [issue](https://github.com/stretchr/testify/issues/425)\n\nFor example, where you might have done...\n```go\n  myMock.On(\"DoSomething\", mock.Anything).Return(error.New(\"\"))\n```\nmmock allows you to do...\n```go\n  myMock.OnMethod(myMock.DoSomething).Return(error.New(\"\"))\n```\n\n## Installation\n\nTo install mmock, use go get:\n\n    go get github.com/go-andiamo/mmock\n\nTo update mmock to the latest version, run:\n\n    go get -u github.com/go-andiamo/mmock\n\n## Usage\n\nTo utilise mmock, simply replace the embedded mock instance in your structure.  For example, where you had...  \n```go\npackage main\n\nimport \"github.com/stretchr/testify/mock\"\n\ntype MyTestObject struct {\n  mock.Mock\n}\n```\nreplace with...\n```go\npackage main\n\nimport \"github.com/go-andiamo/mmock\"\n\ntype MyTestObject struct {\n  mmock.MockMethods\n}\n```\n\nYour mock will now have all the same assertion, on call etc. methods so no other changes are needed - \nbut you'll be able to utilise additional methods to specify mocking methods by function (rather than by name)...\n```go\npackage main\n\nimport (\n  \"errors\"\n  \"github.com/go-andiamo/mmock\"\n  \"github.com/stretchr/testify/assert\"\n  \"testing\"\n)\n\ntype MyInterface interface {\n  DoSomething(a string) (string, error)\n  DoSomethingElse(a int) (int, error)\n}\n\ntype MyTestObject struct {\n  mmock.MockMethods\n}\n\nfunc (m *MyTestObject) DoSomething(a string) (string, error) {\n  args := m.Called(a)\n  return mmock.As[string](args, 0), mmock.As[error](args, 1)\n}\n\nfunc (m *MyTestObject) DoSomethingElse(a int) (int, error) {\n  args := m.Called(a)\n  return mmock.As[int](args, 0), mmock.As[error](args, 1)\n}\n\nfunc TestMyMock_DoSomething(t *testing.T) {\n  mocked := mmock.NewMockOf[MyTestObject, MyInterface]()\n  mocked.OnMethod(mocked.DoSomething).Return(\"\", errors.New(\"foo\"))\n\n  _, err := mocked.DoSomething(\"a\")\n  assert.Error(t, err)\n  mocked.AssertMethodCalled(t, mocked.DoSomething)\n}\n\nfunc TestMyMock_AllMethods(t *testing.T) {\n  mocked := mmock.NewMockOf[MyTestObject, MyInterface]()\n  mocked.OnAllMethods(true) // all methods return an error (where the method returns an error\n\n  _, err := mocked.DoSomething(\"a\")\n  assert.Error(t, err)\n  mocked.AssertMethodCalled(t, mocked.DoSomething)\n  mocked.AssertMethodNotCalled(t, mocked.DoSomethingElse)\n\n  _, err = mocked.DoSomethingElse(1)\n  assert.Error(t, err)\n  mocked.AssertMethodCalled(t, mocked.DoSomethingElse)\n}\n```\nThings to notice:\n* when using `.OnMethod()` you only need to specify as many args that need matching - because\n  mmock knows about the method, it can fill in remaining args with `mock.Anything`\n* the same is true of `.AssertMethodCalled()` and `.AssertMethodNotCalled()` - unspecified args are filled with `mock.Anything`\n* use `.OnAllMethods()` to mock all methods (optionally making all return an error)\n* use `mmock.As()` generic function in your mocked methods to return correct types\n\n## Spy Mocks\nMmock also provides for 'spy mocks' - where an actual underlying implementation is supplied to the mock.\nIf methods on the mock are called but have not been mocked (using `.On()` or `.OnMethod()`) then the underlying method is called - but you can still assert that method was called.   \n\nSee [example](https://github.com/go-andiamo/mmock/tree/main/examples/spy)\n\n## Mock generator\nMmock comes with a programmatic mock generator, e.g.\n```go\n  f, _ := os.Create(\"internal/mock_thingy.go\")\n  _ = mmock.MockGenerateFile[internal.Thingy](\"\", f)\n```\nor...\n```go\n  code, _ := mmock.MockGenerate[internal.Thingy](\"\")\n  println(string(code))\n```\nCaveat emptor! The generated code is designed to save you work but is not 100% guaranteed to produce\ncompilable code (it can get confused with convoluted or conflicting package names) and may sometimes\nrequire manual intervention.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-andiamo%2Fmmock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgo-andiamo%2Fmmock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-andiamo%2Fmmock/lists"}