{"id":26863841,"url":"https://github.com/andreyarthur/mockx","last_synced_at":"2025-03-31T03:32:58.698Z","repository":{"id":284383158,"uuid":"954335389","full_name":"AndreyArthur/mockx","owner":"AndreyArthur","description":"Mockx is a lightweight and intuitive mocking library for Go interfaces. Targeting simplicity and directness.","archived":false,"fork":false,"pushed_at":"2025-03-25T16:03:17.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T16:38:37.064Z","etag":null,"topics":["go","golang","library","mocking","testing"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/AndreyArthur/mockx","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/AndreyArthur.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":"2025-03-24T23:33:09.000Z","updated_at":"2025-03-25T16:07:12.000Z","dependencies_parsed_at":"2025-03-25T16:49:16.266Z","dependency_job_id":null,"html_url":"https://github.com/AndreyArthur/mockx","commit_stats":null,"previous_names":["andreyarthur/mockx"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndreyArthur%2Fmockx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndreyArthur%2Fmockx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndreyArthur%2Fmockx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndreyArthur%2Fmockx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AndreyArthur","download_url":"https://codeload.github.com/AndreyArthur/mockx/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246413363,"owners_count":20773052,"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","library","mocking","testing"],"created_at":"2025-03-31T03:32:58.074Z","updated_at":"2025-03-31T03:32:58.691Z","avatar_url":"https://github.com/AndreyArthur.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mockx\n\nMockx is a lightweight and intuitive mocking library for Go interfaces. It simplifies testing by allowing you to create mock implementations, define method behaviors, and capture method arguments with minimal boilerplate.\n\n- [Motivation](#motivation)\n- [Installation](#installation)\n- [Docs](https://pkg.go.dev/github.com/AndreyArthur/mockx)\n- [Features](#features)\n- [Usage](#usage)\n- [Examples](#examples)\n- [License](#license)\n\n## Motivation\n\nThis section reflects only the author's opinion. Whether you agree or disagree, you are encouraged to continue using the library as you see fit.\n\n- Dissatisfaction with the excessive boilerplate required to create mocks with other libraries, which practically forces the use of code generation.\n- Code generation feels like a hack rather than a proper solution in any sense.\n- Lack of clarity and directness when defining mock behaviors, especially due to the use of expects.\n- Using expects in mocks is conceptually wrong. If you find expects useful in your mocks, you are probably not writing unit tests. Additionally, integration tests should not use mocks.\n- Using a bunch of \"anything\" as expects feels even worse.\n\n## Installation\n\n```bash\ngo get github.com/AndreyArthur/mockx\n```\n\n## Features\n\n- **Automatic Method Initialization**: Generate zero-value implementations for all interface methods.\n- **Method Behavior Mocking**: Override methods with custom implementations or predefined return values.\n- **Argument Capture**: Easily retrieve arguments passed to mocked methods during tests.\n- **Lightweight**: No external dependencies and minimal setup required.\n- **Example-Driven**: Comprehensive examples included in tests for quick learning.\n\n## Usage\n\n### 1. Define Your Interface\n\n```go\ntype Calculator interface {\n    Add(a int, b int) int\n}\n```\n\n### 2. Create a Mock Struct\n\nEmbed `mockx.Mockx` and implement the interface methods using `Call`:\n\n```go\ntype CalculatorMock struct {\n    mockx.Mockx\n}\n\nfunc (m *CalculatorMock) Add(a int, b int) int {\n    values := m.Call(\"Add\", a, b)\n    return mockx.Value[int](values[0])\n}\n```\n\n### 3. Initialize the Mock\n\nUse `Init` to auto-generate zero-value implementations for all methods:\n\n```go\ncalculator := \u0026CalculatorMock{}\ncalculator.Init((*Calculator)(nil)) // Pass a nil interface pointer\n```\n\n### 4. Mock Method Behaviors\n\n#### Set a Custom Implementation\n\n```go\ncalculator.Impl(\"Add\", func(a int, b int) int {\n    return a + b\n})\n```\n\n#### Define Return Values\n\n```go\ncalculator.Return(\"Add\", 42) // Always returns 42\n```\n\n#### Capture Arguments\n\n```go\ncalculator.Add(1, 2)\nargs := calculator.Args(\"Add\") // Returns [1, 2]\n```\n\n## Examples\n\n### Basic Mock Setup\n\n```go\n// Initialize mock and use default zero values\ncalculator := \u0026CalculatorMock{}\ncalculator.Init((*Calculator)(nil))\n\nresult := calculator.Add(3, 4) // Returns 0 (default)\n```\n\n### Override Method Implementation\n\n```go\ncalculator.Impl(\"Add\", func(a, b int) int {\n    return a * b // Change behavior to multiply\n})\n\nresult := calculator.Add(3, 4) // Returns 12\n```\n\n### Force Specific Return Value\n\n```go\ncalculator.Return(\"Add\", 100)\n\nresult := calculator.Add(5, 5) // Returns 100, ignores inputs\n```\n\n### Retrieve Method Arguments\n\n```go\ncalculator.Add(10, 20)\nargs := calculator.Args(\"Add\") // [10, 20]\n```\n\n## License\n\nMIT License. See [LICENSE](LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreyarthur%2Fmockx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandreyarthur%2Fmockx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreyarthur%2Fmockx/lists"}