Skip to content
/ minifake Public

Generate lightweight fake implementations of interfaces to inject as dependencies in tests

Notifications You must be signed in to change notification settings

hoenn/minifake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

minifake

minifake is a tool for generating stubbed implementations, or fakes, of interfaces to inject as dependencies during tests that are as light as possible. Other tools like mockgen and counterfeiter bring in a ton of functionality that is excessive for some projects and make much larger generated fakes. minifake generates the bare minimum.

With this simplistic implementation you can recreate missing features like call counts, thread safety, returns for call, etc., in each stub as needed.

Usage

Use with a go:generate directive:

package foo

//go:generate go run github.com/hoenn/minifake ./filename.go InterfaceA,InterfaceB
type InterfaceA interface {}
// ...
type InterfaceB interface {}

Examples

// testdata.go where an interface is defined.
package testdata

type Job struct {
	name string
}
type JobQueuer interface {
	Enqueue(j *Job) error
	Dequeue(j *Job) error
	Queue() []*Job
}


// testdata_fake.go where the fake is defined.
// Code generated by minifake; DO NOT EDIT.
package testdata

// FakeJobQueuer implements JobQueuer.
type FakeJobQueuer struct {
	EnqueueStub func(j *Job) error
	DequeueStub func(j *Job) error
	QueueStub   func() []*Job
}

func (f *FakeJobQueuer) Enqueue(j *Job) error {
	return f.EnqueueStub(j)
}
func (f *FakeJobQueuer) Dequeue(j *Job) error {
	return f.DequeueStub(j)
}
func (f *FakeJobQueuer) Queue() []*Job {
	return f.QueueStub()
}


// testdata_test.go where we inject our fake for testing and
// define the functions to assert behaviors of the dependent.
func TestJobQueuer(t *testing.T) {
	f := &FakeJobQueuer{
		EnqueueStub: func(j *Job) error {
			// Implement behavior you want to fake, like returning
			// a specific error.
		},
		DequeueStub: func(j *Job) error {
			// Or use a bool to check if a function is ever called.
			// dequeueCalled = true
			// dequeueCalls++
		},
		// Undefined QueueStub() panics when called.
	}
	svc := newComplicatedService(fakeJobQueuer)
	// Expected dequeueCalled to be true
	// Expected some error to happen when queueing
}

About

Generate lightweight fake implementations of interfaces to inject as dependencies in tests

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages