genmocks
generates a mock which satisfies a given interface.
Adapted from https://github.com/josharian/impl
go get -u github.com/delabroj/genmocks
Sample usage:
$ genmocks 'mock *mockReadWriter' io.ReadWriter mocks
// Code generated by genmocks.
// DO NOT EDIT!
package mocks
type mockReadWriter struct {
ReadFunc func(p []byte) (n int, err error)
WriteFunc func(p []byte) (n int, err error)
}
func NewMockReadWriter() *mockReadWriter {
return &mockReadWriter{}
}
func (mock *mockReadWriter) Read(p []byte) (n int, err error) {
return mock.ReadFunc(p)
}
func (mock *mockReadWriter) Write(p []byte) (n int, err error) {
return mock.WriteFunc(p)
}
You can also override any mock method return value to a hardcoded value:
$ genmocks 'mock *mockReadWriter' io.ReadWriter mocks "Read:0, nil"
// Code generated by genmocks.
// DO NOT EDIT!
package mocks
type mockReadWriter struct {
WriteFunc func(p []byte) (n int, err error)
}
func NewMockReadWriter() *mockReadWriter {
return &mockReadWriter{}
}
func (mock *mockReadWriter) Read(p []byte) (n int, err error) {
return 0, nil
}
func (mock *mockReadWriter) Write(p []byte) (n int, err error) {
return mock.WriteFunc(p)
}