Naive code generator that creates mock implementation using testify.mock
.
Unlike mockery, Mocktail generates typed methods on mocks.
For an explanation of why we created Mocktail, you can read this article.
- 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 filemock_test.go
.
package example
// mocktail:MyInterface
You can install Mocktail by running the following command:
go install github.com/traefik/mocktail@latest
You can use pre-compiled binaries:
- To get the binary just download the latest release for your OS/Arch from the releases page
- Unzip the archive.
- Add
mocktail
in yourPATH
.
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
package a
import (
"context"
"time"
)
type Pineapple interface {
Juice(context.Context, string, Water) Water
}
type Coconut interface {
Open(string, int) time.Duration
}
type Water struct{}
package a
import (
"context"
"testing"
)
// mocktail:Pineapple
// mocktail:Coconut
func TestMock(t *testing.T) {
var s Pineapple = newPineappleMock(t).
OnJuice("foo", Water{}).TypedReturns(Water{}).Once().
Parent
s.Juice(context.Background(), "", Water{})
var c Coconut = newCoconutMock(t).
OnOpen("bar", 2).Once().
Parent
c.Open("a", 2)
}
If you need to use your mocks in external packages just add flag -e
:
mocktail -e
In this case, mock will be created in the same package but in the file mock_gen.go
.