Mockgen
Generate mocks of testify/mock from interfaces.
Install
see go install
go install github.com/gleisonmv/mockgen@latest
Generate
Enter in directory and run command to generate the mocks
mockgen
Example
main.go
package main
type User struct {
Id int
Name string
Email string
Password string
}
type UserRepo interface {
FindByEmail(email string) (User, error)
GetAll(page int) ([]User, error)
Delete(id int) error
Lock(id string, isBlock bool) error
IsLocked(id string) (bool, error)
}
Generated
main_mock_test.go
// Code generated by "mockgen"; DO NOT EDIT.
package main
import "github.com/stretchr/testify/mock"
type UserRepoMock struct {
mock.Mock
}
func NewUserRepoMock() *UserRepoMock {
return new(UserRepoMock)
}
func (_mock *UserRepoMock) FindByEmail(email string) (User, error) {
_args := _mock.Called(email)
var _a0 User
if _args.Get(0) != nil {
if v, ok := _args.Get(0).(User); ok {
_a0 = v
}
}
_a1 := _args.Error(1)
return _a0, _a1
}
func (_mock *UserRepoMock) GetAll(page int) ([]User, error) {
_args := _mock.Called(page)
var _a0 []User
if _args.Get(0) != nil {
if v, ok := _args.Get(0).([]User); ok {
_a0 = v
}
}
_a1 := _args.Error(1)
return _a0, _a1
}
func (_mock *UserRepoMock) Delete(id int) error {
_args := _mock.Called(id)
_a0 := _args.Error(0)
return _a0
}
func (_mock *UserRepoMock) Lock(id string, isBlock bool) error {
_args := _mock.Called(id, isBlock)
_a0 := _args.Error(0)
return _a0
}
func (_mock *UserRepoMock) IsLocked(id string) (bool, error) {
_args := _mock.Called(id)
_a0 := _args.Bool(0)
_a1 := _args.Error(1)
return _a0, _a1
}