{"id":13413913,"url":"https://github.com/maxbrunsfeld/counterfeiter","last_synced_at":"2025-04-23T20:57:00.170Z","repository":{"id":17233003,"uuid":"20002077","full_name":"maxbrunsfeld/counterfeiter","owner":"maxbrunsfeld","description":"A tool for generating self-contained, type-safe test doubles in go","archived":false,"fork":false,"pushed_at":"2025-04-10T11:47:21.000Z","size":951,"stargazers_count":1034,"open_issues_count":28,"forks_count":94,"subscribers_count":11,"default_branch":"main","last_synced_at":"2025-04-23T20:56:42.671Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxbrunsfeld.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-05-21T00:12:54.000Z","updated_at":"2025-04-23T05:47:51.000Z","dependencies_parsed_at":"2023-02-18T17:46:12.152Z","dependency_job_id":"1768d86d-16a5-4bdf-a1df-0c78530989eb","html_url":"https://github.com/maxbrunsfeld/counterfeiter","commit_stats":{"total_commits":461,"total_committers":49,"mean_commits":9.408163265306122,"dds":0.7939262472885033,"last_synced_commit":"faa7200a4afe262db1a06c050fce3cf0ecb4b4ec"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxbrunsfeld%2Fcounterfeiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxbrunsfeld%2Fcounterfeiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxbrunsfeld%2Fcounterfeiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxbrunsfeld%2Fcounterfeiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxbrunsfeld","download_url":"https://codeload.github.com/maxbrunsfeld/counterfeiter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250514767,"owners_count":21443208,"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-30T20:01:52.615Z","updated_at":"2025-04-23T20:57:00.150Z","avatar_url":"https://github.com/maxbrunsfeld.png","language":"Go","readme":"# `counterfeiter` [![GitHub Actions](https://github.com/maxbrunsfeld/counterfeiter/actions/workflows/go.yml/badge.svg)](https://github.com/maxbrunsfeld/counterfeiter/actions/workflows/go.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/maxbrunsfeld/counterfeiter/v6)](https://goreportcard.com/report/github.com/maxbrunsfeld/counterfeiter/v6) [![GoDoc](https://godoc.org/github.com/maxbrunsfeld/counterfeiter/v6?status.svg)](https://godoc.org/github.com/maxbrunsfeld/counterfeiter/v6)\n\nWhen writing unit-tests for an object, it is often useful to have fake implementations\nof the object's collaborators. In go, such fake implementations cannot be generated\nautomatically at runtime, and writing them by hand can be quite arduous.\n\n`counterfeiter` allows you to simply generate test doubles for a given interface.\n\n### Supported Versions Of `go`\n\n`counterfeiter` follows the [support policy of `go` itself](https://golang.org/doc/devel/release.html#policy):\n\n\u003e Each major Go release is supported until there are two newer major releases. For example, Go 1.5 was supported until the Go 1.7 release, and Go 1.6 was supported until the Go 1.8 release. We fix critical problems, including [critical security problems](https://golang.org/security), in supported releases as needed by issuing minor revisions (for example, Go 1.6.1, Go 1.6.2, and so on).\n\nIf you are having problems with `counterfeiter` and are not using a supported version of go, please update to use a supported version of go before opening an issue.\n\n### Using `counterfeiter`\n\n⚠️ Please use [`go modules`](https://blog.golang.org/using-go-modules) when working with counterfeiter.\n\nTypically, `counterfeiter` is used in `go generate` directives. It can be frustrating when you change your interface declaration and suddenly all of your generated code is suddenly out-of-date. The best practice here is to use the [`go generate` command](https://blog.golang.org/generate) to make it easier to keep your test doubles up to date.\n\n⚠️ If you are working with go 1.23 or earlier, please refer to an [older version of this README](https://github.com/maxbrunsfeld/counterfeiter/blob/e39cbe6aaa94a0b6718cf3d413cd5319c3a1f6fa/README.md#using-counterfeiter), as the instructions below assume go 1.24 (which added `go tool` support) and later.\n\n#### Step 1 - Create `tools.go`\n\nEstablish a tool dependency on counterfeiter by running the following command:\n\n```shell\ngo get -tool github.com/maxbrunsfeld/counterfeiter/v6\n```\n\n#### Step 2a - Add `go:generate` Directives\n\nYou can add directives right next to your interface definitions (or not), in any `.go` file in your module.\n\n```shell\n$ cat myinterface.go\n```\n\n```go\npackage foo\n\n//go:generate go tool counterfeiter . MySpecialInterface\n\ntype MySpecialInterface interface {\n\tDoThings(string, uint64) (int, error)\n}\n```\n\n```shell\n$ go generate ./...\nWriting `FakeMySpecialInterface` to `foofakes/fake_my_special_interface.go`... Done\n```\n\n#### Step 2b - Add `counterfeiter:generate` Directives\n\nIf you plan to have many directives in a single package, consider using this\noption, as it will speed things up considerably. You can add directives right\nnext to your interface definitions (or not), in any `.go` file in your module.\n\n```shell\n$ cat myinterface.go\n```\n\n```go\npackage foo\n\n// You only need **one** of these per package!\n//go:generate go tool counterfeiter -generate\n\n// You will add lots of directives like these in the same package...\n//counterfeiter:generate . MySpecialInterface\ntype MySpecialInterface interface {\n\tDoThings(string, uint64) (int, error)\n}\n\n// Like this...\n//counterfeiter:generate . MyOtherInterface\ntype MyOtherInterface interface {\n\tDoOtherThings(string, uint64) (int, error)\n}\n```\n\n```shell\n$ go generate ./...\nWriting `FakeMySpecialInterface` to `foofakes/fake_my_special_interface.go`... Done\nWriting `FakeMyOtherInterface` to `foofakes/fake_my_other_interface.go`... Done\n```\n\n#### Step 3 - Run `go generate`\n\nYou can run `go generate` in the directory with your directive, or in the root of your module (to ensure you generate for all packages in your module):\n\n```shell\n$ go generate ./...\n```\n\n#### Invoking `counterfeiter` from the shell\n\nYou can use the following command to invoke `counterfeiter` from within a go module:\n\n```shell\n$ go tool counterfeiter\n\nUSAGE\n\tcounterfeiter\n\t\t[-generate\u003e] [-o \u003coutput-path\u003e] [-p] [--fake-name \u003cfake-name\u003e]\n\t\t[-header \u003cheader-file\u003e]\n\t\t[\u003csource-path\u003e] \u003cinterface\u003e [-]\n```\n\n#### Installing `counterfeiter` to `$GOPATH/bin`\n\nThis is unnecessary if you're using the approach described above, but does allow you to invoke `counterfeiter` in your shell _outside_ of a module:\n\n```shell\n$ go install github.com/maxbrunsfeld/counterfeiter/v6\n$ ~/go/bin/counterfeiter\n\nUSAGE\n\tcounterfeiter\n\t\t[-generate\u003e] [-o \u003coutput-path\u003e] [-p] [--fake-name \u003cfake-name\u003e]\n\t\t[-header \u003cheader-file\u003e]\n\t\t[\u003csource-path\u003e] \u003cinterface\u003e [-]\n```\n\n### Generating Test Doubles\n\nGiven a path to a package and an interface name, you can generate a test double.\n\n```shell\n$ cat path/to/foo/file.go\n```\n\n```go\npackage foo\n\ntype MySpecialInterface interface {\n\t\tDoThings(string, uint64) (int, error)\n}\n```\n\n```shell\n$ go tool counterfeiter path/to/foo MySpecialInterface\nWrote `FakeMySpecialInterface` to `path/to/foo/foofakes/fake_my_special_interface.go`\n```\n\n### Using Test Doubles In Your Tests\n\nInstantiate fakes:\n\n```go\nimport \"my-repo/path/to/foo/foofakes\"\n\nvar fake = \u0026foofakes.FakeMySpecialInterface{}\n```\n\nFakes record the arguments they were called with:\n\n```go\nfake.DoThings(\"stuff\", 5)\n\nExpect(fake.DoThingsCallCount()).To(Equal(1))\n\nstr, num := fake.DoThingsArgsForCall(0)\nExpect(str).To(Equal(\"stuff\"))\nExpect(num).To(Equal(uint64(5)))\n```\n\nYou can stub their return values:\n\n```go\nfake.DoThingsReturns(3, errors.New(\"the-error\"))\n\nnum, err := fake.DoThings(\"stuff\", 5)\nExpect(num).To(Equal(3))\nExpect(err).To(Equal(errors.New(\"the-error\")))\n```\n\nFor more examples of using the `counterfeiter` API, look at [some of the provided examples](https://github.com/maxbrunsfeld/counterfeiter/blob/master/generated_fakes_test.go).\n\n### Generating Test Doubles For Third Party Interfaces\n\nFor third party interfaces, you can specify the interface using the alternative syntax `\u003cpackage\u003e.\u003cinterface\u003e`, for example:\n\n```shell\n$ go tool counterfeiter github.com/go-redis/redis.Pipeliner\n```\n\n### Running The Tests For `counterfeiter`\n\nIf you want to run the tests for `counterfeiter` (perhaps, because you want to contribute a PR), all you have to do is run `scripts/ci.sh`.\n\n### Contributions\n\nSo you want to contribute to `counterfeiter`! That's great, here's exactly what you should do:\n\n- open a new github issue, describing your problem, or use case\n- help us understand how you want to fix or extend `counterfeiter`\n- write one or more unit tests for the behavior you want\n- write the simplest code you can for the feature you're working on\n- try to find any opportunities to refactor\n- avoid writing code that isn't covered by unit tests\n\n`counterfeiter` has a few high level goals for contributors to keep in mind\n\n- keep unit-level test coverage as high as possible\n- keep `main.go` as simple as possible\n- avoid making the command line options any more complicated\n- avoid making the internals of `counterfeiter` any more complicated\n\nIf you have any questions about how to contribute, rest assured that @tjarratt and other maintainers will work with you to ensure we make `counterfeiter` better, together. This project has largely been maintained by the community, and we greatly appreciate any PR (whether big or small).\n\n### License\n\n`counterfeiter` is MIT-licensed.\n","funding_links":[],"categories":["Testing","测试","Template Engines","Go","測試","Mock","测试相关`测试库和测试数据集生成库`","测试相关","\u003cspan id=\"测试-testing\"\u003e测试 Testing\u003c/span\u003e"],"sub_categories":["Mock","HTTP Clients","HTTP客户端","Advanced Console UIs","高級控制台界面","交流","高级控制台界面","查询语","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxbrunsfeld%2Fcounterfeiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxbrunsfeld%2Fcounterfeiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxbrunsfeld%2Fcounterfeiter/lists"}