{"id":13472973,"url":"https://github.com/uber-go/mock","last_synced_at":"2025-05-14T02:08:57.519Z","repository":{"id":166533953,"uuid":"635979215","full_name":"uber-go/mock","owner":"uber-go","description":"GoMock is a mocking framework for the Go programming language.","archived":false,"fork":false,"pushed_at":"2025-04-28T16:32:09.000Z","size":910,"stargazers_count":2764,"open_issues_count":83,"forks_count":140,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-05-11T13:05:42.706Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"golang/mock","license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/uber-go.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-05-03T21:47:38.000Z","updated_at":"2025-05-11T12:24:13.000Z","dependencies_parsed_at":"2023-08-01T08:55:04.568Z","dependency_job_id":"fe46b39b-b180-41b2-88ce-d47685eafbb4","html_url":"https://github.com/uber-go/mock","commit_stats":null,"previous_names":["uber/mock","uber-go/mock"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uber-go%2Fmock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uber-go%2Fmock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uber-go%2Fmock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uber-go%2Fmock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uber-go","download_url":"https://codeload.github.com/uber-go/mock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253568691,"owners_count":21928909,"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":[],"created_at":"2024-07-31T16:00:59.665Z","updated_at":"2025-05-14T02:08:52.506Z","avatar_url":"https://github.com/uber-go.png","language":"Go","readme":"# gomock\n\n[![Build Status][ci-badge]][ci-runs] [![Go Reference][reference-badge]][reference]\n\ngomock is a mocking framework for the [Go programming language][golang]. It\nintegrates well with Go's built-in `testing` package, but can be used in other\ncontexts too.\n\nThis project originates from Google's `golang/mock` repo. Unfortunately, Google\nno longer maintains this project, and given the heavy usage of gomock project\nwithin Uber, we've decided to fork and maintain this going forward at Uber.\n\n[Contributions](./CONTRIBUTING.md) are welcome in the form of GitHub issue or PR!\n\n## Supported Go Versions\n\ngo.uber.org/mock supports all Go versions supported by the official\n[Go Release Policy](https://go.dev/doc/devel/release#policy). That is,\nthe two most recent releases of Go.\n\n## Installation\n\nInstall the `mockgen` tool.\n\n```\ngo install go.uber.org/mock/mockgen@latest\n```\n\nTo ensure it was installed correctly, use:\n\n```\nmockgen -version\n```\n\nIf that fails, make sure your GOPATH/bin is in your PATH. You can add it with:\n\n```\nexport PATH=$PATH:$(go env GOPATH)/bin\n```\n\n## Running mockgen\n\n`mockgen` has two modes of operation: source and package.\n\n### Source mode\n\nSource mode generates mock interfaces from a source file.\nIt is enabled by using the -source flag. Other flags that\nmay be useful in this mode are -imports and -aux_files.\n\nExample:\n\n```bash\nmockgen -source=foo.go [other options]\n```\n\n### Package mode\n\nPackage mode works by specifying the package and interface names.\nIt is enabled by passing two non-flag arguments: an import path, and a\ncomma-separated list of symbols.\n\nYou can use \".\" to refer to the current path's package.\n\nExample:\n\n```bash\nmockgen database/sql/driver Conn,Driver\n\n# Convenient for `go:generate`.\nmockgen . Conn,Driver\n```\n\n### Flags\n\nThe `mockgen` command is used to generate source code for a mock\nclass given a Go source file containing interfaces to be mocked.\nIt supports the following flags:\n\n- `-source`: A file containing interfaces to be mocked.\n\n- `-destination`: A file to which to write the resulting source code. If you\n  don't set this, the code is printed to standard output.\n\n- `-package`: The package to use for the resulting mock class\n  source code. If you don't set this, the package name is `mock_` concatenated\n  with the package of the input file.\n\n- `-imports`: A list of explicit imports that should be used in the resulting\n  source code, specified as a comma-separated list of elements of the form\n  `foo=bar/baz`, where `bar/baz` is the package being imported and `foo` is\n  the identifier to use for the package in the generated source code.\n\n- `-aux_files`: A list of additional files that should be consulted to\n  resolve e.g. embedded interfaces defined in a different file. This is\n  specified as a comma-separated list of elements of the form\n  `foo=bar/baz.go`, where `bar/baz.go` is the source file and `foo` is the\n  package name of that file used by the -source file.\n\n- `-build_flags`: (package mode only) Flags passed verbatim to `go list`.\n\n- `-mock_names`: A list of custom names for generated mocks. This is specified\n  as a comma-separated list of elements of the form\n  `Repository=MockSensorRepository,Endpoint=MockSensorEndpoint`, where\n  `Repository` is the interface name and `MockSensorRepository` is the desired\n  mock name (mock factory method and mock recorder will be named after the mock).\n  If one of the interfaces has no custom name specified, then default naming\n  convention will be used.\n\n- `-self_package`: The full package import path for the generated code. The\n  purpose of this flag is to prevent import cycles in the generated code by\n  trying to include its own package. This can happen if the mock's package is\n  set to one of its inputs (usually the main one) and the output is stdio so\n  mockgen cannot detect the final output package. Setting this flag will then\n  tell mockgen which import to exclude.\n\n- `-copyright_file`: Copyright file used to add copyright header to the resulting source code.\n\n- `-debug_parser`: Print out parser results only.\n\n- `-write_package_comment`: Writes package documentation comment (godoc) if true. (default true)\n\n- `-write_generate_directive`: Add //go:generate directive to regenerate the mock. (default false)\n\n- `-write_source_comment`: Writes original file (source mode) or interface names (package mode) comment if true. (default true)\n\n- `-typed`: Generate Type-safe 'Return', 'Do', 'DoAndReturn' function. (default false)\n\n- `-exclude_interfaces`: Comma-separated names of interfaces to be excluded\n\nFor an example of the use of `mockgen`, see the `sample/` directory. In simple\ncases, you will need only the `-source` flag.\n\n## Building Mocks\n\n```go\ntype Foo interface {\n  Bar(x int) int\n}\n\nfunc SUT(f Foo) {\n // ...\n}\n\n```\n\n```go\nfunc TestFoo(t *testing.T) {\n  ctrl := gomock.NewController(t)\n\n  m := NewMockFoo(ctrl)\n\n  // Asserts that the first and only call to Bar() is passed 99.\n  // Anything else will fail.\n  m.\n    EXPECT().\n    Bar(gomock.Eq(99)).\n    Return(101)\n\n  SUT(m)\n}\n```\n\n## Building Stubs\n\n```go\ntype Foo interface {\n  Bar(x int) int\n}\n\nfunc SUT(f Foo) {\n // ...\n}\n\n```\n\n```go\nfunc TestFoo(t *testing.T) {\n  ctrl := gomock.NewController(t)\n\n  m := NewMockFoo(ctrl)\n\n  // Does not make any assertions. Executes the anonymous functions and returns\n  // its result when Bar is invoked with 99.\n  m.\n    EXPECT().\n    Bar(gomock.Eq(99)).\n    DoAndReturn(func(_ int) int {\n      time.Sleep(1*time.Second)\n      return 101\n    }).\n    AnyTimes()\n\n  // Does not make any assertions. Returns 103 when Bar is invoked with 101.\n  m.\n    EXPECT().\n    Bar(gomock.Eq(101)).\n    Return(103).\n    AnyTimes()\n\n  SUT(m)\n}\n```\n\n## Modifying Failure Messages\n\nWhen a matcher reports a failure, it prints the received (`Got`) vs the\nexpected (`Want`) value.\n\n```shell\nGot: [3]\nWant: is equal to 2\nExpected call at user_test.go:33 doesn't match the argument at index 1.\nGot: [0 1 1 2 3]\nWant: is equal to 1\n```\n\n### Modifying `Want`\n\nThe `Want` value comes from the matcher's `String()` method. If the matcher's\ndefault output doesn't meet your needs, then it can be modified as follows:\n\n```go\ngomock.WantFormatter(\n  gomock.StringerFunc(func() string { return \"is equal to fifteen\" }),\n  gomock.Eq(15),\n)\n```\n\nThis modifies the `gomock.Eq(15)` matcher's output for `Want:` from `is equal\nto 15` to `is equal to fifteen`.\n\n### Modifying `Got`\n\nThe `Got` value comes from the object's `String()` method if it is available.\nIn some cases the output of an object is difficult to read (e.g., `[]byte`) and\nit would be helpful for the test to print it differently. The following\nmodifies how the `Got` value is formatted:\n\n```go\ngomock.GotFormatterAdapter(\n  gomock.GotFormatterFunc(func(i any) string {\n    // Leading 0s\n    return fmt.Sprintf(\"%02d\", i)\n  }),\n  gomock.Eq(15),\n)\n```\n\nIf the received value is `3`, then it will be printed as `03`.\n\n[golang]:              http://go.dev/\n[ci-badge]:            https://github.com/uber-go/mock/actions/workflows/test.yaml/badge.svg\n[ci-runs]:             https://github.com/uber-go/mock/actions\n[reference-badge]:     https://pkg.go.dev/badge/go.uber.org/mock.svg\n[reference]:           https://pkg.go.dev/go.uber.org/mock\n","funding_links":[],"categories":["Test","Testing","Go","测试","Template Engines"],"sub_categories":["Mock"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuber-go%2Fmock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuber-go%2Fmock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuber-go%2Fmock/lists"}