Skip to content

Golang tool to generate mock structs from interfaces to use in unit testing

License

Notifications You must be signed in to change notification settings

mvlipka/imocker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

imocker

Go Reference Go Report Go Coverage

Overview

imocker is a tool designed to generate mock structs that implement an interface in order to effectively write unit tests using nothing but the standard library.

imocker generates actual Go code that can be used in unit tests without using strings to look up methods, without chaining function calls to compare expectations, and no ability to diverge from the interface implementation.

Installation

Go Install

go install github.com/mvlipka/imocker@latest

Usage

imocker
imocker generate ./...
imocker generate ./testdata

Examples

A small example can be seen in the testdata folder.
testdata/mock_thinger.go was generated by using imocker generate ./...

An interface such as

type Thinger interface {
	MyFunc(param string) (string, error)
	NamedReturn(multiple bool, types bool) (err error)
}

Will generate a mock such as:

type MockThinger struct {
	TestMyFunc func(param string) (string, error)
	TestNamedReturn func(multiple bool, types bool) (err error)
}

func (m *MockThinger) MyFunc(param string) (string, error) {
	return m.TestMyFunc(param)
}

func (m *MockThinger) NamedReturn(multiple bool, types bool) (err error) {
	return m.TestNamedReturn(multiple, types)
}

Current Limitations

  • MyMethod(multiple, vars bool) (error)
    • Methods with multiple parameters under a single type definition are unsupported
  • MyMethod(bool, bool) (error)
    • Methods with no parameter names defined are unsupported

About

Golang tool to generate mock structs from interfaces to use in unit testing

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published