{"id":13413923,"url":"https://github.com/gojuno/minimock","last_synced_at":"2025-12-27T12:54:18.681Z","repository":{"id":41583776,"uuid":"64859646","full_name":"gojuno/minimock","owner":"gojuno","description":"Powerful mock generation tool for Go programming language","archived":false,"fork":false,"pushed_at":"2025-02-13T18:50:18.000Z","size":3154,"stargazers_count":665,"open_issues_count":3,"forks_count":40,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-03-11T01:35:12.706Z","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/gojuno.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":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-03T16:01:35.000Z","updated_at":"2025-03-04T10:56:11.000Z","dependencies_parsed_at":"2022-09-16T16:44:13.282Z","dependency_job_id":"e6d6a7bb-3321-4d3e-962c-704f288f1731","html_url":"https://github.com/gojuno/minimock","commit_stats":null,"previous_names":[],"tags_count":52,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gojuno%2Fminimock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gojuno%2Fminimock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gojuno%2Fminimock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gojuno%2Fminimock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gojuno","download_url":"https://codeload.github.com/gojuno/minimock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243642005,"owners_count":20323948,"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.813Z","updated_at":"2025-12-27T12:54:18.674Z","avatar_url":"https://github.com/gojuno.png","language":"Go","readme":"![logo](https://rawgit.com/gojuno/minimock/master/logo.svg)\n[![GoDoc](https://godoc.org/github.com/gojuno/minimock?status.svg)](http://godoc.org/github.com/gojuno/minimock) \n[![Go Report Card](https://goreportcard.com/badge/github.com/gojuno/minimock)](https://goreportcard.com/report/github.com/gojuno/minimock)\n[![Release](https://img.shields.io/github/release/gojuno/minimock.svg)](https://github.com/gojuno/minimock/releases/latest)\n[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/avelino/awesome-go#testing)\n\n\n## Summary \nMinimock generates mocks out of Go interface declarations.\n\nThe main features of minimock are:\n\n* It generates statically typed mocks and helpers. There's no need for type assertions when you use minimock.\n* It's fully integrated with the standard Go \"testing\" package.\n* It's ready for Go modules.\n* It supports generics.\n* It works well with [table driven tests](https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go) because you can set up mocks for several methods in one line of code using the builder pattern.\n* It can generate several mocks in one run.\n* It can generate mocks from interface aliases.\n* It generates code that passes default set of [golangci-lint](https://github.com/golangci/golangci-lint) checks.\n* It puts //go:generate instruction into the generated code, so all you need to do when the source interface is updated is to run the `go generate ./...` command from within the project's directory.\n* It makes sure that all mocked methods have been called during the test and keeps your test code clean and up to date.\n* It provides When and Then helpers to set up several expectations and results for any method.\n* It generates concurrent-safe mocks and mock invocation counters that you can use to manage mock behavior depending on the number of calls.\n* It can be used with the [GoUnit](https://github.com/hexdigest/gounit) tool which generates table-driven tests that make use of minimock.\n\n## Installation\n\nIf you use go modules please download the [latest binary](https://github.com/gojuno/minimock/releases/latest)\nor install minimock from source:\n```\ngo install github.com/gojuno/minimock/v3/cmd/minimock@latest\n```\n\nIf you don't use go modules please find the latest v2.x binary [here](https://github.com/gojuno/minimock/releases)\nor install minimock using [v2 branch](https://github.com/gojuno/minimock/tree/v2)\n\n## Usage\n\n```\n minimock [-i source.interface] [-o output/dir/or/file.go] [-g]\n  -g\tdon't put go:generate instruction into the generated code\n  -h\tshow this help message\n  -i string\n    \tcomma-separated names of the interfaces to mock, i.e fmt.Stringer,io.Reader\n    \tuse io.* notation to generate mocks for all interfaces in the \"io\" package (default \"*\")\n  -o string\n    \tcomma-separated destination file names or packages to put the generated mocks in,\n    \tby default the generated mock is placed in the source package directory\n  -p string \n        comma-separated package names,\n        by default the generated package names are taken from the destination directory names\n  -pr string\n        mock file prefix\n  -s string\n    \tmock file suffix (default \"_mock_test.go\")\n  -gr\n        changes go:generate line from \"//go:generate minimock args...\" to  \n        \"//go:generate go run github.com/gojuno/minimock/v3/cmd/minimock\", \n        useful while controlling minimock version with go mod\n```\n\nLet's say we have the following interface declaration in github.com/gojuno/minimock/tests package:\n```go\ntype Formatter interface {\n\tFormat(string, ...interface{}) string\n}\n```\n\nThis will generate mocks for all interfaces defined in the \"tests\" package:\n\n```\n$ cd ~/go/src/github.com/gojuno/minimock/tests\n$ minimock \n```\n\nHere is how to generate a mock for the \"Formatter\" interface only:\n\n```\n$ cd ~/go/src/github.com/gojuno/minimock/tests\n$ minimock -i Formatter \n```\n\nSame using the relative package notation:\n\n```\n$ minimock -i ./tests.Formatter\n```\n\nSame using the full import path of the source package:\n\n```\n$ minimock -i github.com/gojuno/minimock/tests.Formatter -o ./tests/\n```\n\nAll the examples above generate ./tests/formatter_mock_test.go file\n\n\nNow it's time to use the generated mock. There are several ways it can be done.\n\n### Setting up a mock using the builder pattern and Expect/Return methods:\n```go\nmc := minimock.NewController(t)\nformatterMock := NewFormatterMock(mc).FormatMock.Expect(\"hello %s!\", \"world\").Return(\"hello world!\")\n```\n\nThe builder pattern is convenient when you have more than one method to mock.\nLet's say we have an io.ReadCloser interface which has two methods: Read and Close\n```go\ntype ReadCloser interface {\n\tRead(p []byte) (n int, err error)\n\tClose() error\n}\n```\n\nWe can set up a mock using a simple one-liner:\n```go\nmc := minimock.NewController(t)\nreadCloserMock := NewReadCloserMock(mc).ReadMock.Expect([]byte(1,2,3)).Return(3, nil).CloseMock.Return(nil)\n```\n\nBut what if we don't want to check all arguments of the read method?\nLet's say we just want to check that the second element of the given slice \"p\" is 2.\nThis is where \"Inspect\" helper comes into play:\n```go\nmc := minimock.NewController(t)\nreadCloserMock := NewReadCloserMock(mc).ReadMock.Inspect(func(p []byte){\n  assert.Equal(mc, 2, p[1])\n}).Return(3, nil).CloseMock.Return(nil)\n\n```\n\n### Setting up a mock using ExpectParams helpers:\n\nLet's say we have a mocking interface with function that has many arguments:\n```go\ntype If interface {\n\tDo(intArg int, stringArg string, floatArg float)\n}\n```\n\nImagine that you don't want to check all the arguments, just one or two of them.\nThen we can use ExpectParams helpers, which are generated for each argument:\n```go\nmc := minimock.NewController(t)\nifMock := NewIfMock(mc).DoMock.ExpectIntArgParam1(10).ExpectFloatArgParam3(10.2).Return()\n```\n\n### Setting up a mock using When/Then helpers:\n```go\nmc := minimock.NewController(t)\nformatterMock := NewFormatterMock(mc)\nformatterMock.FormatMock.When(\"Hello %s!\", \"world\").Then(\"Hello world!\")\nformatterMock.FormatMock.When(\"Hi %s!\", \"there\").Then(\"Hi there!\")\n```\n\nalternatively you can use the one-liner:\n\n```go\nformatterMock = NewFormatterMock(mc).FormatMock.When(\"Hello %s!\", \"world\").Then(\"Hello world!\").FormatMock.When(\"Hi %s!\", \"there\").Then(\"Hi there!\")\n```\n\n### Setting up a mock using the Set method:\n```go\nmc := minimock.NewController(t)\nformatterMock := NewFormatterMock(mc).FormatMock.Set(func(string, ...interface{}) string {\n  return \"minimock\"\n})\n```\n\nYou can also use invocation counters in your mocks and tests:\n```go\nmc := minimock.NewController(t)\nformatterMock := NewFormatterMock(mc)\nformatterMock.FormatMock.Set(func(string, ...interface{}) string {\n  return fmt.Sprintf(\"minimock: %d\", formatterMock.BeforeFormatCounter())\n})\n```\n\n### Setting up expected times mock was called:\nImagine you expect mock to be called exactly 10 times. \nThen you can set `Times` helper to check how many times mock was invoked.\n\n```go\nmc := minimock.NewController(t)\nformatterMock := NewFormatterMock(mc).FormatMock.Times(10).Expect(\"hello %s!\", \"world\").Return(\"hello world!\")\n```\n\nThere are also cases, when you don't know for sure if the mocking method would be called or not. \nBut you still want to mock it, if it will be called. This is where \"Optional\" option comes into play:\n\n```go\nmc := minimock.NewController(t)\nformatterMock := NewFormatterMock(mc).FormatMock.Optional().Expect(\"hello %s!\", \"world\").Return(\"hello world!\")\n```\n\nWhen this option is set, it disables checking the call of mocking method. \n\n### Mocking context\nSometimes context gets modified by the time the mocked method is being called.\nHowever, in most cases you don't really care about the exact value of the context argument.\nIn such cases you can use special `minimock.AnyContext` variable, here are a couple of examples:\n\n```go\nmc := minimock.NewController(t)\nsenderMock := NewSenderMock(mc).\n  SendMock.\n    When(minimock.AnyContext, \"message1\").Then(nil).\n    When(minimock.AnyContext, \"message2\").Then(errors.New(\"invalid message\"))\n```\n\nor using Expect:\n\n```go\nmc := minimock.NewController(t)\nsenderMock := NewSenderMock(mc).\n  SendMock.Expect(minimock.AnyContext, \"message\").Return(nil)\n```\n\n### Make sure that your mocks are being used \nOften we write tons of mocks to test our code but sometimes the tested code stops using mocked dependencies.\nYou can easily identify this problem by using `minimock.NewController` instead of just `*testing.T`. \nAlternatively you can use `mc.Wait` helper if your're testing concurrent code.\nThese helpers ensure that all your mocks and expectations have been used at least once during the test run.\n\n```go\nfunc TestSomething(t *testing.T) {\n  // it will mark this example test as failed because there are no calls\n  // to formatterMock.Format() and readCloserMock.Read() below\n  mc := minimock.NewController(t)\n\n  formatterMock := NewFormatterMock(mc)\n  formatterMock.FormatMock.Return(\"minimock\")\n\n  readCloserMock := NewReadCloserMock(mc)\n  readCloserMock.ReadMock.Return(5, nil)\n}\n```\n\n### Testing concurrent code\nTesting concurrent code is tough. Fortunately minimock.Controller provides you with the helper method that makes testing concurrent code easy.\nHere is how it works:\n\n```go\nfunc TestSomething(t *testing.T) {\n  mc := minimock.NewController(t)\n\n  //Wait ensures that all mocked methods have been called within the given time span\n  //if any of the mocked methods have not been called Wait marks the test as failed\n  defer mc.Wait(time.Second)\n\n  formatterMock := NewFormatterMock(mc)\n  formatterMock.FormatMock.Return(\"minimock\")\n\n  //tested code can run the mocked method in a goroutine\n  go formatterMock.Format(\"hello world!\")\n}\n```\n\n## Using GoUnit with minimock\n\nWriting test is not only mocking the dependencies. Often the test itself contains a lot of boilerplate code.\nYou can generate test stubs using [GoUnit](https://github.com/hexdigest/gounit) tool which has a nice template that uses minimock.\n\nHappy mocking!\n","funding_links":[],"categories":["Testing","测试","Go","测试相关","Template Engines","测试相关`测试库和测试数据集生成库`","\u003cspan id=\"测试-testing\"\u003e测试 Testing\u003c/span\u003e","Mock"],"sub_categories":["Mock","HTTP客户端","HTTP Clients","查询语","交流","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%2Fgojuno%2Fminimock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgojuno%2Fminimock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgojuno%2Fminimock/lists"}