{"id":18977953,"url":"https://github.com/becheran/smock","last_synced_at":"2025-04-19T17:34:58.434Z","repository":{"id":152343242,"uuid":"625593324","full_name":"becheran/smock","owner":"becheran","description":"Mock generator for golang ","archived":false,"fork":false,"pushed_at":"2024-02-22T19:19:51.000Z","size":583,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-16T13:35:09.140Z","etag":null,"topics":["assertion","golang","mock","testing","testing-tools"],"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/becheran.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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-04-09T15:38:45.000Z","updated_at":"2023-12-28T16:26:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"20bca9de-d192-4864-8bbf-3664247b6967","html_url":"https://github.com/becheran/smock","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/becheran%2Fsmock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/becheran%2Fsmock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/becheran%2Fsmock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/becheran%2Fsmock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/becheran","download_url":"https://codeload.github.com/becheran/smock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249750287,"owners_count":21320108,"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":["assertion","golang","mock","testing","testing-tools"],"created_at":"2024-11-08T15:31:32.760Z","updated_at":"2025-04-19T17:34:58.419Z","avatar_url":"https://github.com/becheran.png","language":"Go","readme":"# smock\n\n[![Pipeline Status](https://github.com/becheran/smock/actions/workflows/go.yml/badge.svg)](https://github.com/becheran/smock/actions/workflows/go.yml)\n[![Doc][go-doc-image]][go-doc-url]\n[![Go Report Card][go-report-image]][go-report-url]\n[![PRs Welcome][pr-welcome-image]][pr-welcome-url]\n[![License][license-image]][license-url]\n\n[license-url]: https://github.com/becheran/smock/blob/main/LICENSE\n[license-image]: https://img.shields.io/badge/License-MIT-brightgreen.svg\n[go-report-image]: https://goreportcard.com/badge/github.com/becheran/smock\n[go-report-url]: https://goreportcard.com/report/github.com/becheran/smock\n[pr-welcome-image]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg\n[pr-welcome-url]: https://github.com/becheran/smock/blob/main/CONTRIBUTING.md\n[go-doc-image]: https://godoc.org/github.com/becheran/smock?status.svg\n[go-doc-url]: https://godoc.org/github.com/becheran/smock\n\nSimple and fast mock generator for golang\n\n![Logo](./docs/logo.png)\n\n## Features\n\nMocking interfaces for unit tests is a common task in *go* which can be done manually or using a generator which would automates the process of repeatably writing `structs` which fullfil the interfaces for testing.\n\nThere are at least two other popular mock generators that exist for *go* right now. The first is *mockgen* which is part of the [mock](https://github.com/golang/mock) module which was maintained by the *golang* team, but recently moved to a [*uber* as new maintainer](https://github.com/uber-go/mock). The other is [mockery](https://github.com/vektra/mockery) which uses the [testify mock](https://pkg.go.dev/github.com/stretchr/testify/mock) interfaces.\n\nSo why \"yet another mock generator\", or in other words \"what does *smock* offer that *mockgen* or *mockery* doesn't?\"\n\nThe intention of *smock* is to simplify the process of manually mocking interfaces. The tool focused on the following features:\n\n- Can be used as a [library of a project](#library-tool) or as a [standalone tool](#standalone-tool) using `go generate`\n- No boilerplate code such as `gomock.Controller` required to use the mock objects\n- Keep type information of input and return parameter when using the test doubles\n- Clear and intuitive interface for mocked objects. No unnecessary info provided. For example there is no `Return` expression for mocked functions that do not return a value.\n- Fast parsing and generation\n- No complex builtin assertion capabilities. Though, allow them to be added if needed for specific tests\n\n## Setup\n\n### Standalone Tool\n\nInstall latest version:\n\n``` sh\ngo install github.com/becheran/smock@latest\n```\n\nAnnotate `interface` which shall be mocked:\n\n``` go\n//go:generate smock\ntype MockMeIfYouCan interface {\n Foo(bar int, baz string) (res int, err error)\n}\n```\n\nRun the go generate command in the module root directory next to the `go.mod` file to generate mocks for all annotated interfaces.\n\n### Library Tool\n\nUsing *smock* as an installed tool which is the same for all other mocking frameworks has the drawback that mock generation will fail if the tool is not installed on a developer PC as a prerequisite.\n\nInstead of using *smock* as a cli tool it is also possible to add *smock* as a library dependency to a project and still be able to run it via `go generate`.\n\nAdd smock as a dependency to your project:\n\n``` sh\ngo get github.com/becheran/smock\n```\n\nCreate a new main method which will be used to generate mocks. A recommendation is to put it in the `internal` directory to not expose it to the outside. For example `internal/cmd/smock/main.go`. Add the `go:generate` header to allow this method to be run from the `go generate` command. See the [documentation](https://pkg.go.dev/github.com/becheran/smock/smock) for how the mock generation can be configured:\n\n``` go\npackage main\n\nimport \"github.com/becheran/smock/smock\"\n\n//go:generate go run ./\nfunc main() {\n    smock.GenerateMocks()\n}\n```\n\n## Generate Mocks\n\nOnce *smock* is [setup](#setup) the mock objects can be generated from the module root path:\n\n``` sh\ngo generate ./...\n```\n\nAll generated mocks appear in the directory `mocks` next to the corresponding module root path. The import name for the generated mocks will be `\u003cPackageNameOfInterface\u003e_mock`.\n\nA good idea might be to ignore all generated mocks. This can be achieved for example by adding the  following line to your `.gitignore` file:\n\n``` txt\n*/**/*_mock\n```\n\n## Use Mocked Objects\n\nThe mocked interface can be used in unit tests. They have an additional `WHEN` function to set behaviors for each exposed function of the interface. The mock can either `Do` something or `Return` fixed values when a function is called.\n\nThe mocks can act like all types of mock objects [described by martin fowler](https://martinfowler.com/articles/mocksArentStubs.html).\n\n### Dummy\n\nDirectly pass mock to consumer:\n\n``` go\nfunc TestMockMeIfYouCan(t *testing.T) {\n Consumer(foo_mock.NewMockMockMeIfYouCan(t))\n}\n```\n\n### Stub\n\nReturn fixed answers:\n\n``` go\nfunc TestMockMeIfYouCan(t *testing.T) {\n mock := foo_mock.NewMockMockMeIfYouCan(t)\n mock.WHEN().Foo().Return(42, nil)\n Consumer(mock)\n}\n```\n\n### Spy\n\nAssert arguments when being called:\n\n``` go\nfunc TestMockMeIfYouCan(t *testing.T) {\n mock := gomod_test_mock.NewMockMockMeIfYouCan(t)\n mock.WHEN().Foo().Expect(match.Eq(42), nil, match.Not(match.Eq(\"invalid\")))\n Consumer(mock)\n}\n```\n\n### Mock or Fake\n\nDo and return arbitrary stuff when being called:\n\n``` go\nfunc TestMockMeIfYouCan(t *testing.T) {\n mock := gomod_test_mock.NewMockMockMeIfYouCan(t)\n ctr := 0\n mock.WHEN().Foo().Do(func(bar int, baz string) (res int, err error) {\n  ctr++\n  return ctr, nil\n }).Times(2)\n Consumer(mock)\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbecheran%2Fsmock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbecheran%2Fsmock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbecheran%2Fsmock/lists"}