Skip to content

gmhafiz/mirip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mirip is a mock generator to interfaces

Introduction

Largely based on https://github.com/matryer/moq but the mock being generated is based on the guide in https://quii.gitbook.io/learn-go-with-tests/questions-and-answers/http-handlers-revisited

Install with

# Go 1.16+
go install github.com/gmhafiz/mirip/cmd/mirip@latest

# Go <= 1.15
GO111MODULE=on go get -u github.com/gmhafiz/mirip/cmd/mirip@latest

Usage

Either use //go:generate tag or run mirip directly in shell.

Run mirip -help for usage help.

Using Tag

Add the //go:generate tag to run mirip.

//go:generate mirip -rm -out generated.go . MyInterface

// MyInterface is a test interface.
type MyInterface interface {
	One() bool
	Two() int
	Three() string
}

It will generate a mock file:

// Code generated by mirip; DO NOT EDIT.
// https://github.com/gmhafiz/mirip

package generate

import ()

// MyInterfaceMock is a mock implementation of MyInterface.
type MyInterfaceMock struct {
	OneFunc   func() bool
	ThreeFunc func() string
	TwoFunc   func() int
}

func (m *MyInterfaceMock) One() bool {
	return m.OneFunc()
}

func (m *MyInterfaceMock) Three() string {
	return m.ThreeFunc()
}

func (m *MyInterfaceMock) Two() int {
	return m.TwoFunc()
}

From CLI

Run all of your go generate

go generate ./...

Or individually

cd <your interface path>
go generate .