Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/traefik/mocktail
Naive code generator that creates mock implementation using testify.mock
https://github.com/traefik/mocktail
go golang mock mock-generator testify testify-mocking
Last synced: 7 days ago
JSON representation
Naive code generator that creates mock implementation using testify.mock
- Host: GitHub
- URL: https://github.com/traefik/mocktail
- Owner: traefik
- License: apache-2.0
- Created: 2022-06-04T21:03:53.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-05-02T16:38:58.000Z (7 months ago)
- Last Synced: 2024-06-18T22:59:06.763Z (5 months ago)
- Topics: go, golang, mock, mock-generator, testify, testify-mocking
- Language: Go
- Homepage: https://traefik.io/blog/mocktail-the-mock-generator-for-strongly-typed-mocks/
- Size: 149 KB
- Stars: 201
- Watchers: 12
- Forks: 16
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Mocktail
Naive code generator that creates mock implementation using `testify.mock`.
Unlike [mockery](https://github.com/vektra/mockery), Mocktail generates typed methods on mocks.
For an explanation of why we created Mocktail, you can read [this article](https://traefik.io/blog/mocktail-the-mock-generator-for-strongly-typed-mocks/).
## How to use
- Create a file named `mock_test.go` inside the package that you can to create mocks.
- Add one or multiple comments `// mocktail:MyInterface` inside the file `mock_test.go`.```go
package example// mocktail:MyInterface
```
## How to Install
### Go Install
You can install Mocktail by running the following command:
```bash
go install github.com/traefik/mocktail@latest
```### Pre-build Binaries
You can use pre-compiled binaries:
* To get the binary just download the latest release for your OS/Arch from [the releases page](https://github.com/traefik/mocktail/releases)
* Unzip the archive.
* Add `mocktail` in your `PATH`.## Notes
It requires testify >= v1.7.0
Mocktail can only generate mock of interfaces inside a module itself (not from stdlib or dependencies)
The `// mocktail` comments **must** be added to a file named `mock_test.go` only,
comments in other files will not be detected## Examples
```go
package aimport (
"context"
"time"
)type Pineapple interface {
Juice(context.Context, string, Water) Water
}type Coconut interface {
Open(string, int) time.Duration
}type Water struct{}
``````go
package aimport (
"context"
"testing"
)// mocktail:Pineapple
// mocktail:Coconutfunc TestMock(t *testing.T) {
var s Pineapple = newPineappleMock(t).
OnJuice("foo", Water{}).TypedReturns(Water{}).Once().
Parents.Juice(context.Background(), "", Water{})
var c Coconut = newCoconutMock(t).
OnOpen("bar", 2).Once().
Parentc.Open("a", 2)
}
```## Exportable Mocks
If you need to use your mocks in external packages just add flag `-e`:
```shell
mocktail -e
```In this case, mock will be created in the same package but in the file `mock_gen.go`.