{"id":13786643,"url":"https://github.com/pasdam/mockit","last_synced_at":"2025-05-11T22:31:54.101Z","repository":{"id":38357774,"uuid":"231193575","full_name":"pasdam/mockit","owner":"pasdam","description":"Library that make mocking of Go functions/methods easy","archived":true,"fork":false,"pushed_at":"2024-05-24T15:45:48.000Z","size":97,"stargazers_count":16,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-17T22:36:27.196Z","etag":null,"topics":["go","golang","lib","library","mock","mocking","monkey-patching","stub","stubbing","stubs","testing","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pasdam.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":"2020-01-01T08:46:09.000Z","updated_at":"2024-05-24T15:46:29.000Z","dependencies_parsed_at":"2024-06-18T22:53:48.090Z","dependency_job_id":"cfc10a28-4564-4cad-86a2-30763fd961c1","html_url":"https://github.com/pasdam/mockit","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pasdam%2Fmockit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pasdam%2Fmockit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pasdam%2Fmockit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pasdam%2Fmockit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pasdam","download_url":"https://codeload.github.com/pasdam/mockit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253645298,"owners_count":21941314,"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":["go","golang","lib","library","mock","mocking","monkey-patching","stub","stubbing","stubs","testing","unit-testing"],"created_at":"2024-08-03T19:01:24.243Z","updated_at":"2025-05-11T22:31:49.089Z","avatar_url":"https://github.com/pasdam.png","language":"Go","readme":"# mockit\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/pasdam/mockit)](https://goreportcard.com/report/github.com/pasdam/mockit)\n[![CI Status](https://github.com/pasdam/mockit/workflows/Continuous%20integration/badge.svg)](https://github.com/pasdam/mockit/actions)\n[![GoDoc](https://pkg.go.dev/github.com/pasdam/mockit/mockit?status.svg)](https://pkg.go.dev/github.com/pasdam/mockit/mockit)\n\n- [mockit](#mockit)\n  - [Notes](#notes)\n  - [Usage](#usage)\n    - [Argument matcher](#argument-matcher)\n      - [Capture argument](#capture-argument)\n    - [Pausing and restoring a mock](#pausing-and-restoring-a-mock)\n    - [Verify a call](#verify-a-call)\n    - [Update the library](#update-the-library)\n  - [Development](#development)\n    - [TODOs](#todos)\n    - [Contributing](#contributing)\n    - [Internals](#internals)\n  - [Credits](#credits)\n\nMockit is a library to use during testing for Go application, and aim to make\nmocking of functions/methods easy.\n\n## Notes\n\nThis is still a working in progress so **API might change** before reaching a\nstable state.\n\nAlso please note that the **mocking might not work** in some cases if function\ninlining is enabled, so it might be necessary to disable it during testing:\n\n```sh\ngo test -gcflags=-l\n```\n\nFinally this library currently supports `amd64` platforms only, so `ARM` ones\nare not supported yet.\n\n## Usage\n\nTo mock a function:\n\n```go\nm := MockFunc(t, filepath.Base)\nm.With(\"some-argument\").Return(\"result\")\n```\n\nThis will make sure that when `filepath.Base` is called with the argument\n`some-argument`, it will return `result`.\n\nTo mock an instance method (at the moment only exported methods are supported):\n\n```go\nerr := errors.New(\"some-error\")\nm := MockMethod(t, err, err.Error)\nm.With().Return(\"some-other-value\")\n```\n\nTo mock a method for all the instances of a type:\n\n```go\nerr := errors.New(\"some-error\")\nm := MockMethodForAll(t, err, err.Error)\nm.With().Return(\"some-other-value\")\n```\n\nWhen a method is mocked and a matching call is not found (i.e. arguments are\ndifferent) it will return the zero values.\n\nIt is possible to make the mock call the real method:\n\n```go\nm.With(\"some-argument\").CallRealMethod()\n```\n\nor return zero values:\n\n```go\nm.With(\"some-argument\").ReturnDefaults()\n```\n\nMocks are matched in order, which means that:\n\n```go\nm.With(\"some-argument\").CallRealMethod()\nm.With(\"some-argument\").ReturnDefaults()\n```\n\nwill make `filepath.Base(\"some-argument\")` call the real method.\n\nMocks are *automatically removed* when the test is completed.\n\n### Argument matcher\n\nIt is also possible to use argument matchers, to implement generic behaviors. At\nthe moment there is only one matcher implemented, and it matches any argument:\n\n```go\nm := MockFunc(t, filepath.Base)\nm.With(argument.any).Return(\"result\")\n```\n\nThis will make `filepath.Base` return `result` for any input.\n\n#### Capture argument\n\nTo capture the argument of a call:\n\n```go\nm := MockFunc(t, filepath.Base)\nc := argument.Captor{}\nm.With(c.Capture).Return(\"result\")\nfilepath.Base(\"some-argument\")\n```\n\nAt this point `c.Value` will be `some argument`.\n\n### Pausing and restoring a mock\n\nIt is possible to temporary disable a mock:\n\n```go\nm := MockFunc(t, filepath.Base)\nm.With(\"matching-argument\").Return(\"some-out\")\n\n// ... Do something with the mock\n\nm.Disable()\n```\n\nAt this point the mock is disabled and the real implementation is used.\n\nTo enable the mock again, just use:\n\n```go\nm.Enable()\n```\n\n### Verify a call\n\nTo verify a specified call happened:\n\n```go\nm := MockFunc(t, filepath.Base)\n\n// ... Mock calls\n// ... And use mock\n\nm.Verify(\"matching-argument\")\n```\n\nThe `Verify` method will fail the test if the call didn't happen.\n\n### Update the library\n\nTo update the library to the latest version simply run:\n\n```sh\ngo get -u github.com/pasdam/mockit\n```\n\n## Development\n\n### TODOs\n\nThis are (not in a particular order) the missing features that are going to be\nimplemented in a not well defined future (patches are welcome):\n\n- [ ] Mock unexported methods\n- [ ] Mock interfaces\n- [ ] Automatically verify at the end of the test, without having to call\n  `verify` method\n- [ ] [Verify in order calls](https://site.mockito.org/javadoc/current/org/mockito/Mockito.html#in_order_verification)\n- [ ] [Verifying exact number of invocations / at least x / never](https://site.mockito.org/javadoc/current/org/mockito/Mockito.html#at_least_verification)\n- [Arguments matcher](https://site.mockito.org/javadoc/current/index.html?org/mockito/ArgumentMatcher.html)\n  - [ ] IsA: to match for specific types\n  - [ ] NotNil: to match any not nil value\n- [ ] [Stubbing consecutive calls](https://site.mockito.org/javadoc/current/org/mockito/Mockito.html#stubbing_consecutive_calls)\n- [ ] [Stubbing with callbacks](https://site.mockito.org/javadoc/current/org/mockito/Mockito.html#answer_stubs)\n- [ ] Mock [variadic function](https://gobyexample.com/variadic-functions)\n- [ ] Override existing mock, i.e. change return values of a stub\n  - [ ] [Making sure interaction(s) never happened on mock](https://site.mockito.org/javadoc/current/org/mockito/Mockito.html#never_verification)\n  - [ ] [Finding redundant invocations](https://site.mockito.org/javadoc/current/org/mockito/Mockito.html#finding_redundant_invocations)\n- [ ] Improve error messages\n\n### Contributing\n\nRules to contribute to the repo:\n\n1. Define ine identifier per file, which means that each go file contains either\n   a struct (with related methods), an interface, or a function. Constants\n   should be declared in the file `\u003cpackage\u003e.go`, i.e. in `mockit.go` for the\n   `mockit` package.\n2. Write unit test for each method/function, in order to keep the coverage to\n   100%.\n\n### Internals\n\nThis library uses [monkey](https://github.com/bouk/monkey), a package for\n[monkey patching](https://en.wikipedia.org/wiki/Monkey_patch) in Go. And of\ncourse it inherits the [same limitations](https://github.com/bouk/monkey#notes),\nin particular:\n\n\u003e Monkey sometimes fails to patch a function if inlining is enabled. Try running\n\u003e your tests with inlining disabled, for example: go test -gcflags=-l.\n\n## Credits\n\nAll of this was possible only because of [bouk](https://github.com/bouk), as\nthis library is basically a wrapper around\n[monkey](https://github.com/bouk/monkey), so kudos to him.\n","funding_links":[],"categories":["Testing","测试","Mock"],"sub_categories":["Mock","HTTP Clients","HTTP客户端"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpasdam%2Fmockit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpasdam%2Fmockit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpasdam%2Fmockit/lists"}