{"id":19330718,"url":"https://github.com/versent/go-vermock","last_synced_at":"2026-01-12T07:46:40.811Z","repository":{"id":211418175,"uuid":"723844404","full_name":"Versent/go-vermock","owner":"Versent","description":"A mocking framework for Go","archived":false,"fork":false,"pushed_at":"2024-03-22T10:11:16.000Z","size":91,"stargazers_count":4,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T04:47:22.925Z","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":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Versent.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":"2023-11-26T22:26:11.000Z","updated_at":"2024-06-25T04:43:26.000Z","dependencies_parsed_at":"2023-12-22T00:55:56.580Z","dependency_job_id":"f2816c91-5120-4149-a813-4ef049646712","html_url":"https://github.com/Versent/go-vermock","commit_stats":null,"previous_names":["versent/go-mock","versent/go-vermock"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Versent%2Fgo-vermock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Versent%2Fgo-vermock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Versent%2Fgo-vermock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Versent%2Fgo-vermock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Versent","download_url":"https://codeload.github.com/Versent/go-vermock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250340234,"owners_count":21414503,"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-11-10T02:37:42.993Z","updated_at":"2026-01-12T07:46:40.771Z","avatar_url":"https://github.com/Versent.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `vermock` module\n\n[![Go Reference](https://pkg.go.dev/badge/image)](https://pkg.go.dev/github.com/Versent/go-vermock)\n[![Go Report](https://goreportcard.com/badge/github.com/Versent/go-vermock)](https://goreportcard.com/report/github.com/Versent/go-vermock)\n[![Go Coverage](https://github.com/Versent/go-vermock/wiki/coverage.svg)](https://github.com/Versent/go-vermock/wiki/Test-coverage-report)\n\nTired of mocking libraries with cumbersome APIs?  Frustrated with numerous and complicated options?\nLooking for a mock that works well with a composite of small interfaces or loves high ordered\nfunctions?\nIntroducing `vermock`, the simple mocking support that will enthusiastically accept a function that\ncan be tailored to any bespoke test case.\n`vermock` is guided by a central principle: test code must have full control of the code that runs\nin the mocked object.  This means mock behaviour has access to anything in the test fixture and the\n`testing.T` value.\nThis module provides a number functions that can be used as building blocks for your own mocks.\n\n## Installation\n\nTo use the vermock module, ensure it is installed and imported in your project.\n\n```go\nimport vermock \"github.com/Versent/go-vermock\"\n```\n\nTo use the vermockgen command, simply run go run:\n\n```sh\ngo run github.com/Versent/go-vermock/cmd/vermockgen\n```\n\nAfter running vermockgen for the first time, go generate can be used to regenerate generated files:\n\n```sh\ngo generate\n```\n\n## Basic Usage\n\n1. **Define an Interface**\n\n  Create one or more interfaces that your mock needs to satisfy.  For example:\n\n  ```go\n  package my\n\n  type Getter interface {\n  \tGet(key string) (any, bool)\n  }\n\n  type Putter interface {\n  \tPut(key string, value any) error\n  }\n  ```\n\n2. **Create a Mock Implementation**\n\n  Implement the interface with mock methods. For example:\n\n  ```go\n  type mockObject struct {\n  \t_ byte // prevent zero-sized type\n  }\n\n  func (m *mockObject) Get(key string) (any, bool) {\n  \treturn vermock.Call2[any, bool](m, \"Get\", key)\n  }\n\n  func (m *mockObject) Put(key string, value any) error {\n  \treturn vermock.Call1[error](m, \"Put\", key, value)\n  }\n  ```\n\n  Note that `vermock.New` will panic if a zero-sized type is constructed more than once.\n\n3. (Optional) **Define Helpers**\n\n  Implement Expect functions for greater readability. For example:\n\n  ```go\n  func ExpectGet(delegate func(testing.TB, string) (any, bool)) func(*mockObject) {\n  \treturn vermock.Expect[mockObject](\"Get\", delegate)\n  }\n\n  func ExpectPut(delegate func(testing.TB, string, any) error) func(*mockObject) {\n  \treturn vermock.Expect[mockObject](\"Put\", delegate)\n  }\n  ```\n\n4. **Using the Mock in Tests**\n\n  Create a mock instance in your test and use it as needed. For instance:\n\n  ```go\n  func TestObject(t *testing.T) {\n  \tm := vermock.New(t,\n  \tvermock.ExpectGet(func(t testing.TB, key string) (any, bool) {\n  \t\t// Define your mock's behaviour\n  \t}),\n  \tvermock.ExpectPut(func(t testing.TB, key string, value any) error {\n  \t\t// Define your mock's behaviour\n  \t}),\n  )\n\n  \t// Use the mock instance in your test\n\n  \t// Assert that all expected methods were called\n  \tvermock.AssertExpectedCalls(t, m)\n  }\n  ```\n\n### Using vermockgen\n\nAlternatively, creating a mock implementation and associated helpers can be automated with vermockgen.\nInstead of implementing all the methods of your mock, simply declare the interfaces you want your\nmock to satisfy in an ordinary go file and let vermockgen do the rest.\n\nTo continue the example from above this file would look like:\n\n```go\n//go:build vermockstub\n\npackage my\n\ntype mockObject struct {\n\tGetter\n\tPutter\n}\n```\n\nThis is an ordinary go source file with a special build tag: vermockstub.  After running vermockgen (see\nInstallation above) a new file called `vermock_gen.go` will be created with a new definition of\n`mockObject` (the build tag ensures that these two definitions do not collide) containing all the\ngenerated methods and functions.\n\n## Beyond Basic Usage\n\nBe sure to checkout the Examples in the tests.\n\n### Expect Variants\n\nIn addition to the `vermock.Expect` function, which corresponds to a single call of a method,\nthere is also `vermock.ExpectMany`, which will consume all remaining calls of a method.\n\nExpect functions accepts a delegate function that matches the signature of the named method.\nThe delegate may also accept a `*testingT` or `testing.TB` value as the first argument.\nThis the same `testing.T` that was used to construct the mock (first argument to `vermock.New`).\nIn addition, ExpectMany optionally accepts the method's call count.\n\n### Ordered Calls\n\nThe `vermock.ExpectInOrder` will ensure that calls occur in a specified order.\nFor example, this will fail if `Put` is called before `Get`:\n\n```go\nvermock.New(t, vermock.ExpectInOrder(vermock.Expect(\"Get\", ...), vermock.Expect(\"Put\", ...)))\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fversent%2Fgo-vermock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fversent%2Fgo-vermock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fversent%2Fgo-vermock/lists"}