genmocks

command module
v0.0.0-...-e8a9aec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2019 License: MIT Imports: 15 Imported by: 0

README

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)
}

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL